简体   繁体   English

WPF中相同解决方案中不同类库项目中的运行时更改资源文件

[英]Change Resource file at runtime in different class library project in same solution in WPF

I have a solution with these projects: 我与这些项目的解决方案:

1. Clinica (Type is Windows Application),( MainWindow.xaml is here) 1. Clinica (类型为Windows应用程序),( MainWindow.xaml在此处)

2. Ferhad.Wpf.Core (Type is Class Library), (There are three files: Resources.xaml , OrangeResource.xaml , BlueResource.xaml ) 2. Ferhad.Wpf.Core (类型为类库),(三个文件: Resources.xamlOrangeResource.xamlBlueResource.xaml

The Resources.xaml looks like that: Resources.xaml看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/Ferhad.Wpf.Core;component/BlueResource.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now, from MainWindow.xaml.cs I want to clear and add OrangeReource.xaml to Resources.xaml . 现在,我要从MainWindow.xaml.cs清除并将OrangeReource.xaml添加到Resources.xaml

How can i do that: So far I have tried this: 我该怎么做:到目前为止,我已经尝试过了:

string fileName = "pack://application:,,,/Ferhad.Wpf.Core;component/OrangeReSource.xaml";
if (System.IO.File.Exists(fileName))
{
    using (FileStream fs = new FileStream(fileName, FileMode.Open))
    {
        ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);

        string fileNameR = "pack://application:,,,/Ferhad.Wpf.Core;component/Resources.xaml";
        if (System.IO.File.Exists(fileNameR))
        {
            using (FileStream fsR = new FileStream(fileNameR, FileMode.Open))
            {
                ResourceDictionary dicR = (ResourceDictionary)XamlReader.Load(fsR);
                dicR.MergedDictionaries.Clear();
                dicR.MergedDictionaries.Add(dic);
            }
        }
    }
}

Thanks. 谢谢。

I have done the job like that: 我已经完成了这样的工作:

  var rDictionary = new ResourceDictionary();
  rDictionary.Source = new Uri(string.Format("pack://application:,,,/Ferhad.Wpf.SystemStyles;component/OStyles.xaml"), UriKind.Absolute);
  this.Resources.MergedDictionaries.Add(rDictionary);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当启动项目是 class 库时,找不到 WPF ResourceDictionary 转换器资源 - WPF ResourceDictionary converter resource not found when startup project is a class library 在同一解决方案中从另一个项目的视图中调用类库中的函数 - calling function in class library from view in another project in same solution 我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗? - Can I access another project's Embedded Resource from a different project in the same solution? C#运行时更改WPF动态资源 - C# Runtime change WPF Dynamic Resource 钩子和测试项目在不同的项目中,但在相同的解决方案中 - Hooks and test project are in different project but in same solution Visual Studio中的C#类库项目类型缺少菜单中的WPF资源字典项 - C# Class Library project type in Visual Studio missing WPF Resource Dictionary item in menu 在类库项目中访问es-mx资源文件时缺少清单资源异常 - Missing manifest Resource exception while accessing es-mx resource file in class library project 在解决方案中获取类库项目中的连接字符串 - Get connection string in class library project in a solution 在相同解决方案的网站项目中引用的类库中使用Web服务 - Using web service in class library which is referenced in the website project of same solution 在同一个Asp.Net Core 2解决方案中将DbContext注入到类库项目中 - Injecting DbContext to class library project in same Asp.Net Core 2 solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM