You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui如何将NuGet包中的样式引入App.xaml?

解决.NET MAUI引用NuGet包中ResourceDictionary的URI错误

你遇到的问题是因为.NET MAUI不支持WPF/UWP传统的pack://application:,,, URI格式,改用了更简洁的程序集引用方式。下面是正确的处理方法:

方法一:XAML中直接引用

修改App.xaml里的ResourceDictionary引用路径,使用/程序集名称;component/资源相对路径的格式,注意开头的斜杠不能少:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
            <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            <ResourceDictionary Source="/MyPackage.UI;component/Styles/MyStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

方法二:代码方式合并(可选)

如果XAML引用仍有问题,可以在App.xaml.cs的构造函数中通过代码加载资源字典:

public App()
{
    InitializeComponent();
    var externalStyle = new ResourceDictionary
    {
        Source = new Uri("/MyPackage.UI;component/Styles/MyStyle.xaml", UriKind.RelativeOrAbsolute)
    };
    Resources.MergedDictionaries.Add(externalStyle);
    MainPage = new MainPage();
}

关键注意事项

必须确保NuGet包项目中MyStyle.xaml的生成操作设置为EmbeddedResource,否则.NET MAUI无法读取到该资源文件。

内容的提问来源于stack exchange,提问作者HopelesslyLost

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:04:59