Uno 平台初始化材质

Uno Platform initialize Material

我有一个广泛使用 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 是可选的,但如果您要覆盖默认的 Material 颜色,您可以在此处将其设置为定义新调色板的路径。

然后您可以继续从 App.xaml.cs 中删除对 Uno.Material.Resources.Init 的调用。

您可以查看 Uno.Material example 示例,了解您的应用使用新的资源初始化方法的外观。