Andrew
Andrew

Reputation: 147

How to display image in XAML C# through UWP?

I have been looking online, but cannot seem to find out how to add a simple image onto my UWP project. Just looking for it to display properly in my project.

Here is some of my code:

<Image x:Name="img" HorizontalAlignment="Left" Height="27" Margin="1,4,0,0" VerticalAlignment="Top" Width="29" Source="E:\MyTestApp\MyTestApp\Assets\youtube-subscribe-button-2.png"/>

Upvotes: 2

Views: 7104

Answers (1)

Decade Moon
Decade Moon

Reputation: 34286

You cannot set the source of the image to the path you have used. As far as I know, you cannot reference any arbitrary file on the disk to be used as the source for the image (at least not via a path in XAML).

You can reference images that you have added to your project in XAML. Based on that path, it looks like you've already done that (because it's in your Assets directory).

If your XAML Page is in the same directory as the Assets directory, then you can use a relative path:

<Image Source="Assets/youtube-subscribe-button-2.png" />

or you can use an absolute path which doesn't matter where your XAML Page file is located:

<Image Source="ms-appx:///Assets/youtube-subscribe-button2.png" />

See Image.Source property and How to load file resources (XAML)

Upvotes: 3

Related Questions