简体   繁体   中英

MergedDictionaries in XAML rewritten to code

Is this XAML code in 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.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>

equivalent to:

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>

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



One side note: Do I need to call Application.Current.Resources.MergedDictionaries.Clear(); in this situation before i start adding the merged dictionaries? I think at this moment the default MergedDictionaries collection is initially empty.

Ans1) Yes absolutely. They are same.

Ans2) No you do not need to Clear() them for there are no MergedDictionaries and hence Count is 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM