簡體   English   中英

Uno 平台初始化材質

[英]Uno Platform initialize 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 庫最近對材質資源的初始化方式進行了重大更改。 展望未來,資源初始化應該通過 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是可選的,但如果您要覆蓋默認材質 colors,您可以在此處將其設置為定義新調色板的路徑。

然后,您可以提前 go 並從App.xaml.cs中刪除對Uno.Material.Resources.Init的調用。

您可以查看Uno.Material 示例,了解使用新的資源初始化方法時您的應用可能會是什么樣子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM