2012-03-16 5 views
14

WPF 응용 프로그램 인 C# 프로젝트가 있는데 이제는 DLL로 빌드하려고합니다. 이전에 app.xaml을 프로젝트에서 제거하고 빌드 유형을 dll로 설정하여이 작업을 수행했습니다.코드에서 응용 프로그램 리소스 설정

문제는 app.xaml에 응용 프로그램 변수를 인스턴스화하는 일부 xaml이 있다는 것입니다. 이 라운드를 얻으려면 프로그래밍 방식으로 호출 할 첫 번째 xaml 창 내에서 이러한 응용 프로그램 변수를 설정하려면 노력하고있어.

내가 코드를 모방하려고 오전 XAML은 다음과 같습니다

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

해야하는이 작품 :

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

이것은 내가 가지고있는 코드는?

Toolbar.xaml에서 Shared.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에서 참조 어디 이드는 최대한 많은 정보를 제공합니다. 네가 필요한 것이 있으면 알려줘.

+0

, 벡해야 My.Wpf.Component

을 넣어한다 더 이상'App.xaml'을 사용하지 않고 코드를 작성하여 회피 할 수 있습니다. * https://stackoverflow.com/questions/4441227/wpf-class-library-with-resource-dictionary/4441500#4441500에서 여전히 App.xaml을 사용할 수 있습니다. 그런 다음 호출자 DLL은 https://stackoverflow.com/a/26890426/1429390 에서처럼'App'을 instanciate합니다. 상대 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) }); 
관련 문제