.NET MAUI 能够使用 SVG 图像,这非常好,但我无法设置 SVG 图像的颜色。官方文档声明我可以在项目文件中使用 TintColor 但这不是一个好的解决方案,因为我希望能够根据某些条件使用不同的颜色。那么我们能以某种方式指定 SVG 图像的颜色吗?
使用 Xaminals 示例,我正在尝试设置默认启动页面。这是来自 xaml 的相关代码:
<FlyoutItem Route="animals" x:Name="shellAnimals"
Title="Animals"
FlyoutDisplayOptions="AsMultipleItems">
<Tab Title="Domestic" x:Name="shellDomestic"
Route="domestic"
Icon="paw.png">
<ShellContent Route="cats" x:Name="shellCats"
Style="{StaticResource DomesticShell}"
Title="Cats"
Icon="cat.png"
ContentTemplate="{DataTemplate views:CatsPage}" />
<ShellContent Route="dogs" x:Name="shellDogs"
Style="{StaticResource DomesticShell}"
Title="Dogs"
Icon="dog.png"
ContentTemplate="{DataTemplate views:DogsPage}" />
</Tab>
<ShellContent Route="monkeys" x:Name="shellMonkeys"
Style="{StaticResource MonkeysShell}"
Title="Monkeys"
Icon="monkey.png"
ContentTemplate="{DataTemplate views:MonkeysPage}" />
<ShellContent Route="elephants" x:Name="shellElephants"
Style="{StaticResource ElephantsShell}"
Title="Elephants"
Icon="elephant.png"
ContentTemplate="{DataTemplate views:ElephantsPage}" />
<ShellContent Route="bears" x:Name="shellBears"
Style="{StaticResource BearsShell}"
Title="Bears"
Icon="bear.png"
ContentTemplate="{DataTemplate views:BearsPage}" />
</FlyoutItem>
Run Code Online (Sandbox Code Playgroud)
在后面的代码中我可以成功设置默认启动页面。
例如,狗页面:
shellAnimals.CurrentItem.CurrentItem = shellDogs;
Run Code Online (Sandbox Code Playgroud)
但在熊页面上:
// This works, but I don't like …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 Xamarin Forms 应用程序,它有两个项目,一个 Android 应用程序和一个 iOS 应用程序。所有其他代码都存储在共享项目中。
解决方案如下所示:
我使用以下代码从共享项目中读取 SVG 图像:
using (var stream = GetType().Assembly.GetManifestResourceStream("MyApp.Base.image.svg"))
{
// do work here...
}
Run Code Online (Sandbox Code Playgroud)
当图像位于 Android 或 iOS 项目中时,这非常有效,但我希望共享图像,因此我将其放在共享项目中。
GetType().Assembly 返回 MyApp.Android,因此找不到图像。我想我忽略了一些事情,但我一直无法找到解决方案。
有人能指出我正确的方向吗?谢谢!