我在將控件特定資源(特別是Style
)添加到定義了合併ResourceDictionary
的控件時遇到問題。如果重要,我的XAML文件位於我正在工作的主項目的子文件夾中。添加Silverlight樣式和ResourceDictionary時出現XamlParseException
我開始:
<controls:ChildWindow.Resources>
<Style x:Key="ParmatersText" TargetType="TextBlock">
<Setter Property="Margin" Value="5,5"/>
</Style>
</controls:ChildWindow.Resources>
但是當我加入這個有一個問題:
<controls:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="my.foo.bar;component/MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style x:Key="ParmatersText" TargetType="TextBlock">
<Setter Property="Margin" Value="5,5"/>
</Style>
</controls:ChildWindow.Resources>
隨着The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection.
錯誤消息從這個SO Question收視率最高的答案告訴我嘗試以下操作,這會改變</ResourceDictionary>
標記的位置。這other SO question和那裏的答案鏈接支持這種方法。
<controls:ChildWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="my.foo.bar;component/MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ParmatersText" TargetType="TextBlock">
<Setter Property="Margin" Value="5,5"/>
</Style>
</ResourceDictionary>
</controls:ChildWindow.Resources>
然而,失敗與XamlParseException到Failed to assign to property 'System.Windows.ResourceDictionary.Source'
從同樣的問題的影響,我想這給了我這個標記的答案:
<controls:ChildWindow.Resources>
<ResourceDictionary x:Key="MyDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="my.foo.bar;component/MyResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<Style x:Key="ParmatersText" TargetType="TextBlock">
<Setter Property="Margin" Value="5,5"/>
</Style>
</controls:ChildWindow.Resources>
然而,失敗的與之前一樣的XamlParseException。
這兩篇關於XAML Resources和Merged Resource Dictionaries的MSDN文章幫助我理解頁面所隱含的ResourceDictionary
,而且顯然通過在隱式字典中定義資源以及指定的合併文檔來創建衝突。
我的問題是上述語法不起作用,我正在拾取XamlParseExceptions。