在Xamarin中查找iOS项目本地映像的路径

zwghvu4y  于 2022-12-07  发布在  iOS
关注(0)|答案(1)|浏览(132)

我有一个图像视图,如下所示:

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

然后,我想为Android和iOS环境声明两种不同的路径:

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

问题是,我不知道如何加载我的图像从资产目录在iOS项目(Android去好).
有人能帮我吗?

cigdeys3

cigdeys31#

如果您使用的是Android的本机资源和iOS的资产目录,则仅使用文件名作为源。
例如:

原生项目中的档案路径:

  • 安卓系统- /资源/可绘制-{size}/security.png
  • iOS - /资产.xcassets/安全.图像集/{名称/大小}.{png/pdf/json}

使用XAML:

<Image Source="security" />

使用C#:

Image.Source = "security";

(XAML)如果Android和iOS的文件名不同,则使用OnPlatform + On标记,但值仍将仅为不带扩展名的名称。

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

对于C#,可以使用

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

相关问题