我正在尝试为 MAUI 的初始设置创建 C# 实用程序,以确保设置过程不会分散在 5 个文件中。为此,我需要将以下内容转换为 C# 代码:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
到目前为止我已经尝试了两种方法:
var converter = new ResourceDictionary.RDSourceTypeConverter();
try
{
var c = converter.ConvertFromString(resourcepath);
}
catch (Exception ex)
{
// thrown not implemented exception
}
resourceDictionary.MergedDictionaries.Add(new()
{
Source = new Uri(resourcepath, UriKind.Relative) // throws exception saying I need to use XAML
});
有没有其他方法可以创建不影响性能的资源字典?
您绝对可以实例化一个
ResourceDictionary
并将其动态添加到应用程序资源的 MergedDictionaries
。
但是,这通常可以在某些地方起作用(从我自己的存储库之一中的 App.xaml.cs 文件中获取和修改):
if (Current?.Resources.MergedDictionaries is not { } mergedDictionaries)
{
return;
}
mergedDictionaries.Clear();
mergedDictionaries.Add(new global::Resources.Styles.Colors());
mergedDictionaries.Add(new global::Resources.Styles.Styles());
请注意,这些资源字典是编译的。您不能仅将它们作为路径来引用。不过,我不知道这是否真的比默认方式更快。
更新
为此,您需要更新
Colors.xaml
和 Styles.xaml
仅 XAML 资源字典,并使用 x:Class
属性为它们指定类名称:
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Resources.Styles.Colors">
</ResourceDictionary>
然后您可以使用
new global::Resources.Styles.Colors()
和 new global::Resources.Styles.Styles()
实例化它们。
更新2
如果您提供
x:Name
的 ResourceDictionary
一个完全限定的名称(例如 MyProject.Resources.Styles.Colors
),然后在不使用 global::
的情况下实例化它,则可能也有效,如下所示:
mergedDictionaries.Add(new Resources.Styles.Colors());
进一步阅读
官方文档还展示了如何动态添加资源字典(用于为应用程序主题化,但原理是相同的):https://learn.microsoft.com/en-us/dotnet/maui/user-interface/theming ?view=net-maui-7.0#在运行时加载主题
对于那些希望在 MAUI 中使用资源字典而不使用 xaml 页面(仅 C#)的人,这就是我让它工作的方式。
注意:我的项目中唯一的 xaml 页面是默认的 Resources\Styles\Styles.xaml 和 Resources\Styles\Colors.xaml
为 xaml 资源页面创建代码隐藏文件。
namespace MyApp.Resources.Styles;
public partial class Styles : ResourceDictionary
{
public Styles()
{
InitializeComponent();
}
}
然后修改xaml从
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?> <-- REMOVE THIS SO THE CODE-BEHIND FILE WILL COMPLIE
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
到
<?xml version="1.0" encoding="UTF-8" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Resources.Styles.Styles"> <-- ADD FULLY QUALIFIED CLASS NAME
然后添加 App.cs 构造函数(按照 Julian 建议)
if (Application.Current?.Resources.MergedDictionaries is not { } mergedDictionaries)
{
return;
}
mergedDictionaries.Clear();
mergedDictionaries.Add(new Resources.Styles.Colors());
mergedDictionaries.Add(new Resources.Styles.Styles());
在 C# 页面构造函数中使用(例如:MainPage.cs)
Application.Current.Resources.TryGetValue("BtnStyle", out object btnStyle);
然后到控件
new Button
{
Text = "Click Me",
Style = (Style)btnStyle as Style,
}
请记住向 xaml 资源定义中定义的任何样式添加 x:Key。 文件样式.xaml
<Style x:Key="BtnStyle" TargetType="Button">
<Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource White}, Dark={StaticResource Primary}}" />
************* OMITTED SETTERS ***************
</Setter>
</Style>