2009-10-30 29 views
7

Có thể tham chiếu các tài sản Xaml được lưu trữ trong ResourceDictionary (xây dựng action = resource) từ một dự án khác không? Tôi muốn hợp nhất các tài sản vào từ điển tài nguyên của dự án chính hoặc truy cập chúng riêng lẻ. Ví dụ:Truy cập tài nguyên trong Xaml qua các dự án/dlls

  • Dự án "MyResources" chứa một thư mục có tên "Tài sản" trong đó có một ResourceDictionary gọi là "MyAssets.xaml", trong đó có một phong cách gọi là "ButtonStyle"
  • Dự án "MainProject" tài liệu tham khảo MyResources; trong MainWindow.xaml

Trong MainWindow.xaml Tôi muốn làm điều gì đó như:

<ResourceDictionary.MergedResources> 
    <ResourceDictionary Source="/MyResources/Assets/MyAssets.xaml"/> 
</ResourceDictionary.MergedResources> 

Hoặc, nếu đó là không thể, có lẽ:

<Button Style="{StaticResource /MyResources/Assets/MyAssets.xaml}"/> 

Có cách nào để tham khảo nội dung trong MyResources từ MainProject?

Trả lời

13

Theo ResourceDictionary in a separate assembly

<ResourceDictionary.MergedResources> 
    <ResourceDictionary Source="pack://application:,,,/YourAssembly;component/Subfolder/YourResourceFile.xaml"/> 
</ResourceDictionary.MergedResources> 
+0

Cảm ơn bạn, tìm kiếm của tôi đã bật lên. Đây là một bản dupe. –

3

Bạn có thể kết hợp các nguồn lực từ các dự án của bạn vào từ điển chính bằng cách sử dụng phương pháp này:

/// <summary> 
/// Loads a resource dictionary from a URI and merges it into the application resources. 
/// </summary> 
/// <param name="resourceLocater">URI of resource dictionary</param> 
public static void MergeResourceDictionary(Uri resourceLocater) 
{ 
    if (Application.Current != null) 
    { 
     var dictionary = (ResourceDictionary) Application.LoadComponent(resourceLocater); 
     Application.Current.Resources.MergedDictionaries.Add(dictionary); 
    } 
} 

Gọi nó như thế này:

MergeResourceDictionary(new Uri(@"/MyOtherAssembly;component/MyOtherResourceDictionary.xaml", UriKind.Relative));               
+0

Cảm ơn bạn đã cung cấp câu trả lời dựa trên mã, nếu tôi có thể đánh dấu câu trả lời thứ hai tôi sẽ có! –

11
<ResourceDictionary Source="/Commons;component/Themes/TreeStyle.xaml" /> 

đâu:

Commons là tên của dự án bên ngoài

/Themes/TreeStyle.xaml tương ứng với vị trí của file phong cách trong dự án Commons

; thành phần là luôn yêu cầu

+1

Cảm ơn bạn đã giải thích M EI PIECE của câu đố ... +1 –

Các vấn đề liên quan