Home > Mobile >  Find path of local image of iOS project in Xamarin
Find path of local image of iOS project in Xamarin

Time:08-26

I have an Image view like this:

<Image Source="{StaticResource AppIcon}"  WidthRequest="80" HeightRequest="180" IsEnabled="True" ></Image>

Then i want to declare two different paths for either Android and iOS environment as:

<ResourceDictionary>
   <OnPlatform x:TypeArguments="ImageSource" x:Key="AppIcon">
       <On Platform="Android" Value="/Resources/drawable/splash_logo.png" />
       <On Platform="iOS" Value="????" />
   </OnPlatform>
</ResourceDictionary>
        

The problem is that i don't know how load my image from assets catalog in iOS project (android goes ok).

Can anyone help me?

CodePudding user response:

if you are using native resources for Android and Asset Catalog for iOS then use only the name of file as source.


For example:

Files path in native projects:

  • Android - /resource/drawable-{size}/security.png
  • iOS - /Assets.xcassets/security.imageset/{name/size}.{png/pdf/json}

Using XAML:

<Image Source="security" />

Using C#:

Image.Source = "security";

(XAML) If the name of file is different for Android and iOS then use the OnPlatform On tags but value will be still only the name without extension.

<On Platform="Android" Value="security" />
<On Platform="iOS" Value="devices" />

For C# you can use

if (Device.Platform == Device.Android)
  Image.Source = "security";
else
  Image.Source = "devices";
  • Related