Tôi có thư viện, CommonLibraryWpfThemes, với một số tệp XAML từ điển tài nguyên trong đó. Các tệp My Themes/Generic.xml chứa một khai báo ResourceDictionary.MergedDictionaries kết hợp tất cả các tệp khác với nhau.Sự cố khi tham chiếu một Từ điển tài nguyên có chứa từ điển được hợp nhất
Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Trong dự án ứng dụng của tôi, tôi có một tham chiếu đến CommonLibraryWpfThemes, và tôi tham khảo một cách rõ ràng Generic.xml trong file App.xaml tôi.
App.xaml - KHÔNG
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</Application.Resources>
</Application>
này không hoạt động. Tôi nhận được lỗi sau khi tôi chạy ứng dụng của tôi:
System.Windows.Markup.XamlParseException occurred
Message="Cannot find resource named '{_fadedOrangeBrush}'. Resource names are case sensitive. Error at object 'System.Windows.Setter' in markup file 'CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml' Line 18 Position 13."
Source="PresentationFramework"
LineNumber=18
LinePosition=13
Nếu tôi đặt nội dung của Generic.xaml vào App.xaml trực tiếp, mọi thứ hoạt động tốt:
App.xaml - thành công
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/BrushDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/TextBlockDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/LabelDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/ButtonDictionary.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/ResourceDictionaries/WindowDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Có thể tôi sẽ nói về điều này một cách sai. Mục tiêu của tôi là giúp dễ dàng tham khảo tất cả tài nguyên chủ đề của tôi từ nhiều ứng dụng mà không phải liệt kê tất cả các tệp riêng lẻ. Có cách nào được khuyến nghị để làm điều này không? (Lưu ý: Tôi không cố chuyển đổi giữa nhiều chủ đề - tôi chỉ có một chủ đề.)
Là phần thưởng, sẽ rất tuyệt nếu ai đó có thể cho tôi biết cách tham chiếu tài nguyên trong thư viện bên ngoài mà không vi phạm thiết kế trong Visual Studio.
Cảm ơn.
EDIT:
tôi đã cố gắng gói các ResourceDictionary trong một phần tử ResourceDictionary.MergedDictionary, nhưng điều đó cũng không làm việc (tôi nhận được lỗi tương tự):
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/CommonLibraryWpfThemes;component/Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Tôi sẽ bắt đầu một khoản tiền thưởng nhỏ tại đây. Kết hợp một vài RessourceDictionaries vào một Theme (và chỉ tải nó trong App.xaml) có vẻ là một kịch bản đủ phổ biến ... – Jens