c# - XAML 中的 MergedDictionaries 重写为代码

标签 c# wpf xaml styles mergeddictionaries

App.xaml 中的 XAML 代码:

<Application x:Class="Company.Product.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
    <Application.Resources>
      <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/System.Windows.xaml"/>
            <ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.xaml"/>
            <ResourceDictionary Source="/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.Data.xaml"/>               
          </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
    </Application.Resources>
</Application>

相当于:

App.xaml:

<Application x:Class="Company.Product.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
</Application>

App.xaml.cs 中:

public partial class App : Application
{
    public App()
    {               
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Company.Windows.Themes.Theme1;component/Themes/Company.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
        });
    }
}



旁注:我是否需要调用 Application.Current.Resources.MergedDictionaries.Clear();在这种情况下,在我开始添加合并词典之前?我认为此时默认 MergedDictionaries 集合最初是空的。

最佳答案

Ans1) 是的。他们是一样的。

Ans2) 不,你不需要 Clear() 它们因为没有 MergedDictionaries 因此 Count0.

关于c# - XAML 中的 MergedDictionaries 重写为代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17884724/

相关文章:

c# - 创建实体后编辑 CRM 实体。 CRM 动态插件

c# - TripleDES TransformFinalBlock 偶尔会给出 'Bad Data. ' 错误

c# - 使 Command 的 CanExecute 依赖于其他字段的值

c# - [名称]View.xaml.cs 文件中应该包含哪些内容?

xaml - 在 xamarin 表单中实现可重用元素

c# - 我如何识别未被垃圾收集的不需要的 .NET 对象?

C# WPF 如何拥有多个 DataContext

WPF 向 TextBlock 添加边框

xaml - WPF、自定义控件和样式/主题继承

c# - C# 中的 ControlChars 类