2012-03-16 8 views
14

私はWPFアプリケーションであるc#プロジェクトを持っていますが、今はdllとしてビルドしたいと考えています。私はこれまで、プロジェクトからapp.xamlを削除し、そのビルドタイプをdllに設定しました。コードからアプリケーションリソースを設定する

問題は、app.xamlにアプリケーション変数をインスタンス化するためのxamlが含まれていることです。これをラウンドするには、これらのアプリケーション変数を呼び出される最初のxamlウィンドウ内からプログラムで設定しようとしています。

私はコードでエミュレートしようとしているXAMLは次のとおりです。

<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> 

これは私が持っているコードです:

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

は、この作業は必要がありますか?

Shared.xamlで宣言されているリソースが参照されているにもかかわらず、Toolbar.xamlがそのリソースを参照していて、取得できず、次のエラーが表示されるという問題が発生しました。ここで

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

リソースが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> 

をしてdelcaredされている場合、ここでその質問が、思考のエッセイのための

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

申し訳toolbar.xamlで参照のWHERE私はできるだけ多くの情報を提供しています。あなたが何か他のものが必要な場合は教えてください。

+0

、BEC​​でなければなりませんMy.Wpf.Component

を置く必要がありますあなたはもはや 'App.xaml'を使用せず、回避策としてコードを書いています。 * https://stackoverflow.com/questions/4441227/wpf-class-library-with-resource-dictionary/4441500#4441500のままで、あなたは* App *が引き続き 'App.xaml'を使うことができます。次に、呼び出し側のDLLはhttps://stackoverflow.com/a/26890426/1429390のように 'App'をインスタンス化します。相対的なURIのおかげで、すべてのリソースが動作し、時間と実行時間を設計できます。 –

答えて

19

制御します。ただ、相対的にURIを変更:

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

別のResourceDictionaryファイルを作成する必要があります。

<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> 

終わりはあなたのすべてでそれを参照含まStyle.xamlは(名前空間を忘れないように)このコードは、私の作品

+1

私は彼(または彼女)がコードでそれをしたいと思うと思います。 – NestorArturo

+0

コードからそのようなマークアップを作成し、ビジュアルツリーに追加することができます – pamidur

5

私はあなたがコンポーネントの名前を指定する必要があると思うあなたのDLLがMy.Wpf.Component.dll命名されている場合は、リソースが

<ResourceDictionary Source="/<YourDllName>;component/Resources/Styles/Shared.xaml" /> 

に座っているし、あなたはとてもコードで、それはあなたの質問は、コードを書くことについて尋ねる

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/<YourDllName>;component/Resources/Styles/Shared.xaml", UriKind.Relative) }); 
関連する問題