簡體   English   中英

WPF中相同解決方案中不同類庫項目中的運行時更改資源文件

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

我與這些項目的解決方案:

1. Clinica (類型為Windows應用程序),( MainWindow.xaml在此處)

2. Ferhad.Wpf.Core (類型為類庫),(三個文件: Resources.xamlOrangeResource.xamlBlueResource.xaml

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>

現在,我要從MainWindow.xaml.cs清除並將OrangeReource.xaml添加到Resources.xaml

我該怎么做:到目前為止,我已經嘗試過了:

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);
            }
        }
    }
}

謝謝。

我已經完成了這樣的工作:

  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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM