Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c# - Set up application resources from code

I have a c# project that was a WPF application but I now want to build it as a dll. I have previously done this by removing the app.xaml from the project and setting its build type to dll.

The issue I have now is that the app.xaml contains some xaml to instantiate the application variables. To get round this I am trying to programmatically set these application variables from within the first xaml window that will be called.

The xaml I am trying to emulate in code is:

<Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources/Styles/Shared.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Expander.xaml"/>
        <ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/>
        <ResourceDictionary Source="Resources/DesignerItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/>
        <ResourceDictionary Source="Resources/Connection.xaml"/>
        <ResourceDictionary Source="Resources/Slider.xaml"/>
        <ResourceDictionary Source="Resources/ScrollViewer.xaml"/>
        <ResourceDictionary Source="Resources/StatusBar.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

This is the code I have:

ResourceDictionary myResourceDictionary = new ResourceDictionary();
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\Shared.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\ToolBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\GroupBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\ZoomBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\ScrollBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\Expander.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\ApplicationToolbar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\DesignerItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\ToolboxItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Styles\Toolbox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Connection.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\Slider.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\ScrollViewer.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\Resources\StatusBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

Should this work?

I'm hitting a problem in that Toolbar.xaml references a resource declared in Shared.xaml but its not getting picked up and im getting the following error.

Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.

Here is where the resource is delcared in shared.xaml

<LinearGradientBrush x:Key="ToolbarSelectedBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#FFFEE3" Offset="0.0"/>
        <GradientStop Color="#FFE797" Offset="0.4"/>
        <GradientStop Color="#FFD750" Offset="0.4"/>
        <GradientStop Color="#FFE796" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>

and here's where its referenced in toolbar.xaml

<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />

Sorry for the essay of a question but thought id provide as much info as I could. Let me know if you need anything else.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This code works for me. I just changed the URIs to relative:

ResourceDictionary myResourceDictionary = new ResourceDictionary();

myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

myResourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...