Reputation: 143
I'm a new developer trying to learn to develop for Windows 10 UWP and I've been struggling with the basics for days. I'm just trying to figure out how to change XAML properties using C#, in particular I'd like to change the source of an image so that clicking a button will change the image displayed. I'm used to javascript and jQuery which make this process absolutely painless ($("#elementID").attr("src", "Images/NameOfFile.PNG");). How do I do this for a UWP app? It seems like data binding is similar but I'm having a lot of difficulty understanding it and can't imagine that doing something so simple would have to be so difficult. Can you help me with this? Thanks!
Upvotes: 1
Views: 1452
Reputation: 128060
The most simple way (without data binding) is to directly access the Image control in code behind. Give the control a name
<Image x:Name="image"/>
and set its Source property in code behind, e.g. in a Button Click handler
image.Source = new BitmapImage(new Uri("ms-appx:///Images/NameOfFile.png"));
where NameOfFile.png
must be added to a folder named Images
in your Visual Studio project.
See class Uri for details about the ms-appx://
scheme.
Upvotes: 2