2010-06-25 90 views
5

在WPF應用程序,你可以把你的全局靜態資源的App.xaml ..喜歡WPF類庫中的全局靜態資源?

<Application.Resources> 
     <!--Global View Model Locator--> 
     <vm:ViewModelLocator x:Key="Locator" 
          d:IsDataSource="True" /> 
    </Application.Resources> 

這是從MVVM光)。現在,如果您的項目是一個wpf類庫,那麼初始化此類全局靜態資源的正確方法是什麼?

回答

3

您可以使用您的資源創建ResourceDictionary,並使用您的代碼合併字典,如下所示。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:vm="clr-namespace:WPFProject.ViewModel" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<vm:ViewModelLocator x:Key="Locator" 
         d:IsDataSource="True" /> 

代碼:

Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(
      new Uri("/WPFProject;Component/Resources/ResourceDictionary1.xaml", UriKind.Relative)) as ResourceDictionary); 
+1

您可以創建在XAML你MergedDicionaries,在App.xaml文件,這是最常見的做法(而不是在代碼中創建它們後面)。 – Hannish 2013-06-15 10:34:25