链接内容不适用于资源词典

发布于 2024-10-12 20:13:05 字数 2045 浏览 2 评论 0原文

因此,在我们的 Styles.xaml 文档中,我们使用外部 xml 来提供样式的颜色。我们认为这将提供良好的抽象级别,允许非编码人员修改我们应用程序的外观。

它的工作原理如下:

<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:CalManv4UI"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
 mc:Ignorable="d"
 >

  <XmlDataProvider x:Key="BrandInfo" Source="/Config/BrandInfo.xml" XPath="BrandRoot" />

    <Style TargetType="{x:Type TextBox}">
  <Setter Property="Foreground" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxForeground}"/>
        <Setter Property="Background" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxBackground}"/>
  <Setter Property="FontSize" Value="11"/>
  <Setter Property="FontFamily" Value="Verdana"/>
 </Style>
</ResourceDictionary>

这工作正常,但我们需要添加几个辅助应用程序来重用主应用程序的样式。

所以我引用了主应用程序,这样我就可以访问所有代码,然后重用样式。但是当我这样做时,我得到一个 IOException“无法找到资源'config/brandinfo.xml'。”。我仔细检查了brandinfo.xml是否正在被复制,因为mainApp被引用了,所以我很困惑。

我想尝试的下一件事是将其用作链接文件,因此我创建了一个配置文件夹并添加为链接,始终将其设置为内容复制。在我的 csproj 文件中创建此代码

<ItemGroup>
 <Content Include="..\MainAppUI\Config\BrandInfo.xml">
  <Link>Config\BrandInfo.xml</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

仍然不起作用,所以最后我将其添加为文件。这会在我的 csproj 文件中创建此代码。

<ItemGroup>
 <Content Include="Config\BrandInfo.xml">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

所以这可行,但现在我有两份 BrandInfo 文件的副本,这可能会成为今后的耐心问题。

So in our Styles.xaml doc we use an external xml to provide the colors for the styles. We thought this would provide a good level of abstractions to allow people who weren't coders to modify the look of our app.

It works something this:

<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:CalManv4UI"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
 mc:Ignorable="d"
 >

  <XmlDataProvider x:Key="BrandInfo" Source="/Config/BrandInfo.xml" XPath="BrandRoot" />

    <Style TargetType="{x:Type TextBox}">
  <Setter Property="Foreground" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxForeground}"/>
        <Setter Property="Background" Value="{Binding Source={StaticResource BrandInfo}, XPath=//Colors/@TextBoxBackground}"/>
  <Setter Property="FontSize" Value="11"/>
  <Setter Property="FontFamily" Value="Verdana"/>
 </Style>
</ResourceDictionary>

And this works and all is well, but we needed to add a couple of helper apps that would reuse the style of the main app.

So I was referencing the main app so that I can get to all the code goo, and then also reuse the styles. But when I do this I get an IOException "Cannot locate resource 'config/brandinfo.xml'.". I double checked that the brandinfo.xml is being copied over since the mainApp is refrenced, so I was puzzled.

The next thing I thought I'd try is using it as a linked file, so I created a config folder and added as link, set it to content copy always. Which creates this code in my csproj file

<ItemGroup>
 <Content Include="..\MainAppUI\Config\BrandInfo.xml">
  <Link>Config\BrandInfo.xml</Link>
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

Still doesn't work, so finially I added it as a file. Which creates this code in my csproj file.

<ItemGroup>
 <Content Include="Config\BrandInfo.xml">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 </Content>
</ItemGroup>

So this works, but now I have two copies of my BrandInfo file, which could be a problem for matienance going forward.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2024-10-19 20:13:05

我也有同样的问题。幸运的是,这个 其中有一条线索 - 链接的文件位于根目录中。

所以,假设我有以下结构:
我的程序集 ->我的文件夹 -> MyDictionary.xaml

要加载字典形式 App.xaml,请执行以下操作:

  1. 如果 MyDictionary.xaml 是“真实”文件 -

    <资源字典>
    
    
    
    
    
  2. 如果 MyDictionary.xaml 是链接文件 -

    <资源字典>
    
    
    
    
    

(注意:省略 MyFolder)

警告:我在未连接到互联网的计算机上测试了代码,并且代码片段是用 Notepad++ 编写的,因此拼写或语法可能会出现问题下车吧。

I had the same problem. Luckily, this had a clue in it - the linked file is in the root.

So, lets say I have the following structure:
MyAssembly -> MyFolder -> MyDictionary.xaml

To load the dictionary form App.xaml do:

  1. if MyDictionary.xaml is a "real" file -

    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyFolder/MyDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    
  2. if MyDictionary.xaml is a linked file -

    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ReosurceDictionary Source="pack://application:,,,/MyAssembly;component/MyDictionary.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

(NOTE: MyFolder is ommited)

Warning: I tested the code on a machine not connected to the internet and the snippets are written in Notepad++ so spelling or syntax may be off.

穿越时光隧道 2024-10-19 20:13:05

我正在尝试寻找解决此问题的方法。我们遇到了什么问题?问题是在设计模式下 Uri

"/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml"

是正确的,

但在运行时我们必须将其替换为

"/UBP.Documents.RWS;component/ThirdPartyDocumentResourceDictionary.xaml"

是否可以自动执行此操作?这就是我正在考虑的:
我想让 MyResourceDictionary 类继承自标准 ResourceDictionary 并根据我们是否处于 DesignTime 进行替换。

粗略的代码如下所示:

 public class MyResourceDictionary : ResourceDictionary
    {

        new public Uri Source
        {
            get 
            {
                return base.Source;                
            }
            set
            {
                if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
                {
                    base.Source = value;
                }
                else
                {
                    string originalString = value.OriginalString;
                    string newString = originalString.Substring(0, originalString.IndexOf(";component") + 11) + originalString.Substring(originalString.LastIndexOf("/") + 1);                    
                    base.Source = new Uri(newString, UriKind.Relative);
                    base.Source = value;
                }                
            }
        }       
    }

Xaml 看起来像

  <ResourceDictionary.MergedDictionaries>
                <localconverters:MyResourceDictionary Source="/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

并且在运行时它可以工作,但在设计模式下不行(((
接下来,我在调试设计模式时发现,在 setter 中发生在线

base.Source = value;

异常,它说“无法识别 URI 前缀”。

我很好奇我的“价值”是什么:

"markup://1/file:///D:/Svn/2.08.0016_20121116/Development/Sources/Universal/UBP.Documents.RWS/Presentation/ProbateCaseView.xaml%230//UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml}".

这不是我应该看到的(可能是因为我对 Uri 类的基本了解)。

我看到除了资源字典的路径(“/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml”)之外,还有一个带有我的应用程序的 XAML 文件路径的前缀,该行写在其中。
谁能解释一下如何根据字符串将此 Uri 转换为我的相对 Uri

"/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml"

或者您可以提供更优雅的解决方案?

I'm trying to find walkaround for this issue. What problem have we got? The problem is that in Design Mode Uri

"/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml"

is correct

but in the runtime we must substitute it for

"/UBP.Documents.RWS;component/ThirdPartyDocumentResourceDictionary.xaml"

Is it possible to do this automatically? And that is I'm thinking of:
I want to make MyResourceDictionary class inherited from standard ResourceDictionary and make substitutions based on whether we are in DesignTime or not.

Rough code looks like:

 public class MyResourceDictionary : ResourceDictionary
    {

        new public Uri Source
        {
            get 
            {
                return base.Source;                
            }
            set
            {
                if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
                {
                    base.Source = value;
                }
                else
                {
                    string originalString = value.OriginalString;
                    string newString = originalString.Substring(0, originalString.IndexOf(";component") + 11) + originalString.Substring(originalString.LastIndexOf("/") + 1);                    
                    base.Source = new Uri(newString, UriKind.Relative);
                    base.Source = value;
                }                
            }
        }       
    }

Xaml looks like

  <ResourceDictionary.MergedDictionaries>
                <localconverters:MyResourceDictionary Source="/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>

and in runtime it works, but not in design mode (((
The next i fugured out while Debugging Design mode that in setter on line

base.Source = value;

exception occurs, it says "The URI prefix is not recognized."

I'm curious what's in my "value":

"markup://1/file:///D:/Svn/2.08.0016_20121116/Development/Sources/Universal/UBP.Documents.RWS/Presentation/ProbateCaseView.xaml%230//UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml}".

It is not what I'm supposed to see (may be because of my rudimentary knowledge of Uri class).

I see except of my path to Resource dictionary ("/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml") there is a prefix with path to XAML file of my application, where this line ith written.
Could anyone explain me how to convert this Uri to my relative Uri, based on string

"/UBP.Documents.RWS;component/Resources/ThirdPartyDocumentResourceDictionary.xaml"

Or may be you could offer more elegant solution?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文