Ilhan
Ilhan

Reputation: 1331

Why does my xamarin android display images and UWP doesn't?

I have a very strange situation that I've lost countless hours on and I couldn't find anything on stackoverflow either.

When I create a simple image through C# on a blank page something like:

mainStackLayout.Children.Add(new Image() { Source = "mypic.png" });
Content = mainStackLayout;

After I compile this and run on my android device it works perfectly while building and running on UWP doesn't display any image at all.

I copied the same image in UWP/Assets and UWP/ root directory but still nothing. Any ideas?

Upvotes: 0

Views: 219

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

I copied the same image in UWP/Assets and UWP/ root directory but still nothing. Any ideas?

When you copied the pictures over from Android\Drawables to Assets folder in UWP. And BuildAction Property also be copied to the UWP project. So you need to set property as Content.

Otherwise, the pic path is Assets/xxx.png. It can't be accessed with `Source = "xxx.png". So you need to modify the image source.

mainStackLayout.Children.Add(new Image() { Source = "Assets/icon.png" });
Content = mainStackLayout;

For more flexibility the Device.RuntimePlatform property can be used to select a different image file or path for some or all platforms, as shown in this code example

mainStackLayout.Children.Add(new Image() { Source = Device.RuntimePlatform == Device.UWP ? ImageSource.FromFile("Assets/xxx.png") : ImageSource.FromFile("waterfront.jpg")});

For more you could refer working with image.

Upvotes: 2

Related Questions