[英]WPF Prism - Where to put Resources?
我有一個棱鏡應用程序和各種模塊。 我想知道找到資源的最佳位置在哪里,如樣式,畫筆,控制模板,數據模板?
我應該制作一個單一的資源字典並將所有內容放在那里 每個模塊應該有自己的資源嗎? 還是每個視圖? 我想遵循Prism的目標,即保持一切模塊化,但我也沒有看到在每個模塊中重新聲明相同資源的重點......
我用Prism開發應用程序,並且我使用的技術非常接近Prism手冊中描述的。 有YourApplication.Infrastructure項目,您通常放置所有共享接口等。所以:
使用像這樣的內容創建文件Themes \\ 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="Generic.Brushes.xaml"/> <ResourceDictionary Source="Generic.WPF.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
現在你可以在任何模塊中添加這些資源(你有單獨的項目,對嗎?)通過將YourApplication.Resources的引用添加到該項目並添加到視圖的xaml:
<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> <!-- Put your not shared resource here --> </ResourceDictionary> </UserControl.Resources>
我不知道,也許這種方式有一些問題,但它有效,對我來說效果很好。 如果有人可以這種方式評論(優點/缺點) - 我會很高興聽到它!
我通常放在ResourceDictionary
應用程序范圍的ResourceDictionary
,它被添加到App.xaml
或StartupWindow.xaml
特定View的資源通常位於View中。 例如,用於CalendarView的UserControl將包含日歷的任何自定義資源,例如特定於日歷的畫筆,樣式,模板等。
我通常看不到制作模塊范圍資源的理由,但是如果我做的話,我會為模塊提供一個ResourceDictionary
,它可以在運行時加載到應用程序的合並字典中,或者包含在模塊中的各個視圖中。
我想分享一些新的知識。 我正在使用@chopikadze方法。 這真的很酷。 謝謝你!
但是,如果你不想每次為每個控件寫這些代碼:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- Put your not shared resource here -->
</ResourceDictionary>
</UserControl.Resources>
然后你就可以在你的Bootstrapper
App.xaml
中聲明<ResourceDictionary/>
:
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/YourApplication.Resources;component/Themes/Generic.xaml"/>
</Application.Resources>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.