资源字典共享项目(xaml) [英] Shared project with resource dictionary (xaml)

查看:66
本文介绍了资源字典共享项目(xaml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在项目之间共享 ResourceDictionary 的方法.

I am looking for a way to share ResourceDictionary between projects.

向共享项目中添加新项目不提供资源字典.可以在其他(主)项目中创建并拖动它.但后来我无法将其构建选项更改为 Page :

Adding new item to shared project doesn't offer resource dictionary. It can be created in other (main) project and dragged. But then I can't change its build options to Page:

想法是像这样加载资源字典

The idea is to load resource dictionary like this

var dictionary = new ResourceDictionary();
dictionary.Source = new Uri("/WpfApplication91;component/Dictionary2.xaml", UriKind.Relative);

目前这显然是失败的

PresentationFramework.dll中发生了'System.IO.IOException'类型的异常,但未在用户代码中处理

An exception of type 'System.IO.IOException' occurred in PresentationFramework.dll but was not handled in user code

其他信息:无法找到资源"dictionary2.xaml".

Additional information: Cannot locate resource 'dictionary2.xaml'.

有什么想法吗?

推荐答案

可以手动编辑共享项目以设置资源字典的构建操作.

It's possible to manually edit shared project to set build action for resource dictionary.

共享项目由 Project.shproj Project.projitems 文件组成,第二个打开并在其中找到字典:

Shared project consists of Project.shproj and Project.projitems files, open second and locate dictionary there:

<ItemGroup>
  <None Include="$(MSBuildThisFileDirectory)Dictionary.xaml" />
</ItemGroup>

在此之后添加

<ItemGroup>
  <Page Include="$(MSBuildThisFileDirectory)Dictionary.xaml">
    <Generator>MSBuild:Compile</Generator>
    <SubType>Designer</SubType>
  </Page>
</ItemGroup>

这是从正常的 csproj 中复制/粘贴的东西,用于包含字典的WPF项目.

it's a copy/paste thing from normal csproj for WPF project containing dictionary.

似乎可以正常工作,尽管在将项目加载到Visual Studio中时看不到此生成操作.将文件添加到共享项目不会影响此手动更改.

Seems to work, though this build action is not visible when project is loaded into Visual Studio. Adding files to shared project doesn't affect this manual change.

现在我可以共享包含资源字典的项目了,是的!

Now I can have shared project containing resource dictionary, yay!

资源字典可以合并到应用程序字典中,就像它位于项目的根目录中一样(在xaml设计器中用作静态/动态资源):

Resource dictionary can be merged into application dictionaries like if it's located in the root of the project (to use as static/dynamic resource in xaml designer):

<Application.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <!-- doesn't really exists in project -->
            <ResourceDictionary Source="Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

和/或手动加载,例如使用此打包Uri :

and/or loaded manually, e.g. using this pack Uri :

var dictionary = new ResourceDictionary()
{
     Source = new Uri("pack://application:,,,/FlexProperty.xaml"),
};

这篇关于资源字典共享项目(xaml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆