[英]SilverLight Control Library - Cannot Reference Relative MergedDictionary by Generic.xaml
我為此而死。 我花了無數小時試圖找出答案,但沒有運氣。
問題簡短說明
在我的自定義控件類中,當我檢查Application.Current.Resources [“ key”]時,我返回null。 這種“鍵”樣式位於本地字典中,該字典應該由我的控制庫的theme / generic.xaml資源與Application.Current.Resources合並。
如何在SilverLight控件庫的theme / generic.xaml中引用/確認對MergedDictionary的引用。
這是否有可能,或者我對合並資源的想法被認為是完全錯誤的?
請幫忙。 提前致謝。
問題的詳細解釋
我有一個帶有控件文件夾和主題文件夾的Silverlight控件庫。 在主題文件夾中,我具有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="/SilverLightLib;component/Themes/EnhancedLabelDict.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
在主題文件夾中,我具有EnhancedLabelDict.xaml。 其內容:
<Style x:Key="ReadOnlyTextBox" TargetType="TextBox">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="#FFFFFFFF"/>
<Setter Property="Foreground" Value="#FF000000"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="BorderBrush">
<!-- A lot more code -->
</Style>
這兩個文件構建操作均設置為“頁面”。
現在我不知道是否generic.xaml甚至正在加載我的資源。 我唯一能分辨的方法是是否在之間放置一些未格式化的文本。 這會導致錯誤。
如果我使用的ResourceDictionary路徑不正確,則會收到運行時錯誤-“無法分配給屬性'System.Windows.ResourceDictionary.Source”
在我的Controls文件夾中,我具有EnhancedLabel.cs,它擴展了ContentControl。 在其構造函數中,我創建了一個新的TextBox並為其分配樣式,如下所示:
Style style = Application.Current.Resources["ReadOnlyTextBox"] as Style;
this.textBox.Style = style;
我在庫中的App.xaml和EnhancedLabelDict.xaml中都具有這種樣式。 當我注釋掉App.xaml中的樣式時,找不到“ ReadOnlyTextBox”樣式(空)。 取消注釋,找到它。
我不明白為什么無法在我的EnhancedLabel.cs中引用樣式。
如果我使用EnhancedLabelDict.xaml,請將其添加到主應用程序內Resources文件夾內的Themes文件夾中。 如果然后將以下內容添加到我的App.xaml中:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SilverLightPOC;component/Resources/Themes/EnhancedLabelDict.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
我的控制有效! 因此,除了路徑之外,沒有什么不同。 但這是行不通的,因為我不想在主應用程序內部存儲庫所依賴的字典文件。
請幫忙。
當嵌套字典的深度超過3個級別時,Silverlight中會出現一個優化錯誤-除非使用workarround,否則不會加載它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.