如何解决Uno 平台初始化材质
我有一个广泛使用 Material 的应用。最近对 Material 进行了更新并查看了文档 - 他们更改了 Material 的初始化方式。这是我之前在 app.xaml.cs 的 onLaunched 方法中添加的代码:
this.Resources.MergedDictionaries.Add(new Uno.Material.MaterialColorPalette());
this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("ms-appx:///MaterialColorPaletteOverride.xaml") });
this.Resources.MergedDictionaries.Add(new Uno.Material.MaterialResources());
查看 Uno Platform Material How To 上的更新文档
初始化更改为以下内容:
Uno.Material.Resources.Init(this,null);
我试过了,Visual Studio 告诉我资源在命名空间 Uno.Material 中不存在。我还查看了示例应用示例,结果很相似:
Uno.Material.Resources.Init(this,new ResourceDictionary { Source = new Uri("ms-appx:///MaterialColorPaletteOverride.xaml") });
显然它遇到了同样的问题——资源不存在——确切的错误是 Uno.Material 中不存在资源方法。我已经验证了其他的 Uno 包是最新的。我也安装了 Xamarin.AndroidX.Lifecycle.LiveData。在这次更新 Material 之前 - 一切都按预期工作。具体更新到1.0.0-dev.778。我已经恢复到 1.0.0-dev.774 并将我的代码恢复到我第一次列出的三行 - 它再次按预期工作。我应该怎么做才能实施新的更改?
解决方法
Uno.Material 库最近对 Material 资源的初始化方式进行了重大更改。展望未来,资源初始化应通过 XAML 完成,类似于我们为 WinUI 初始化 <XamlControlsResources />
的方式。
文档正在更新中,但基本上您需要像这样将初始化移动到您的 App.xaml
中:
<Application x:Class="Uno.Themes.Samples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:material="using:Uno.Material">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Load WinUI resources -->
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<MaterialColors xmlns="using:Uno.Material"
ColorPaletteOverrideSource="ms-appx:///ColorPaletteOverride.xaml" />
<MaterialResources xmlns="using:Uno.Material" />
<!-- Rest of your application resources .... -->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
注意新的 <MaterialColors />
和 <MaterialResources />
标签。请记住,这里的顺序很重要,MaterialColors
必须在 MaterialResources
之前初始化。
ColorPaletteOverrideSource
是可选的,但如果您要覆盖默认材质颜色,则应在此处将其设置为定义新调色板的路径。
然后您可以继续从您的 Uno.Material.Resources.Init
中删除对 App.xaml.cs
的调用。
您可以查看 Uno.Material example 以了解使用新的资源初始化方法后您的应用程序可能是什么样子。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。