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

如何在UWP项目的App.xaml或MainPage.xaml中共享Common项目的Resources.xaml?

如何在UWP的App.xaml/MainPage.xaml中引用其他项目的Resources.xaml

当然可以实现,只需完成项目引用配置和资源字典合并两步操作,具体步骤如下:

1. 添加项目引用

  • 右键启动项目Main.csproj → 选择「添加」→「项目引用」
  • 在引用窗口中勾选Common.csproj,点击确认完成引用

2. 确认资源文件的生成操作

  • 右键Common.csproj里的Resources.xaml → 打开「属性」面板
  • 确保「生成操作」设置为Resource(默认通常是这个选项,无需修改,确认即可)

场景一:在App.xaml中全局引用(全应用可用)

打开App.xaml,在<Application.Resources>节点内添加合并字典,引用Common项目的资源文件:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- 引用Common项目根目录的Resources.xaml -->
            <ResourceDictionary Source="ms-appx:///Common/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <!-- 此处可放置当前应用的本地资源 -->
    </ResourceDictionary>
</Application.Resources>

说明:ms-appx:///是UWP的绝对URI方案,格式为ms-appx:///[目标项目名称]/[资源文件路径],如果资源在子文件夹中,比如Common/Assets/Resources.xaml,则URI需改为ms-appx:///Common/Assets/Resources.xaml

场景二:在MainPage.xaml中局部引用(仅当前页面可用)

打开MainPage.xaml,在<Page.Resources>节点内添加合并字典:

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///Common/Resources.xaml" />
        </ResourceDictionary.MergedDictionaries>

        <!-- 此处可放置当前页面的本地资源 -->
    </ResourceDictionary>
</Page.Resources>

资源使用

完成配置后,即可像使用本地资源一样,通过{StaticResource 资源名}或{DynamicResource 资源名}调用Resources.xaml中定义的资源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 10:36:24