在WPF中,通过将标记保存在themes文件夹中的单独XAML文件中,然后使用MergedDictionaries将它们导入generic.xaml,可以为多个用户控件组织XAML:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyFirstControl.xaml" />
<ResourceDictionary Source="MySecondControl.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
随着Silverlight 3 beta的推出,引入了合并字典支持,似乎可以对Silverlight用户控件执行相同的操作。但是,尽管在merge.xaml中尝试了合并字典文件的构建操作的所有组合以及源引用的相应语法,但我似乎无法使其工作。
还有其他人试过吗?有谁知道这是否可能,如果是,我做错了什么?
好的 - 所以在经过大量的测试项目之后,在WPF中获取工作样本并将XAML和C#代码移到Silverlight 3并且它仍然失败,我完全卸载并重新安装所有Silverlight 2位和所有Silverlight 3 beta位,最后让事情有效。
我只能假设我最终以错误的方式安装了测试版 - 我不知道但是看起来我仍然在Silverlight 2运行时运行,尽管显然安装了版本3运行时。
感谢Jared看看事情并与SL3团队核实......并感谢Amy Dullard和Shawn Wildermuth生成运行Silverlight 2&amp; 2的指令和批处理文件。 3在同一台机器上。
答案 0 :(得分:12)
我刚刚在用户控件中尝试了以下操作并且它有效:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ResourcesA.xaml" />
<ResourceDictionary Source="ResourcesB.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel>
<Rectangle Width="100" Height="100" Fill="{StaticResource ResAColor}" />
<Rectangle Width="100" Height="100" Fill="{StaticResource ResBColor}" />
</StackPanel>
但你特别提到generic.xaml。你有什么问题?
- 编辑
根据其他评论,我与SL3团队进行了交谈,得到了以下答案:
适用于我,使用编译为资源的generic.xaml,并使用完整资源语法。有一个错误,无法在generic.xaml(31783)中使用Source的相对URI,但非相对表单应该可以正常工作
<ResourceDictionary Source='/SilverlightClassLibrary1;component/CustomControl.xaml'/>
在generic.xaml中,并将generic.xaml和CustomControl.xaml的构建操作修改为Resource。让我知道如果还有问题 - 如果你得到一个复制品,我可以看看它。
这有帮助吗?
答案 1 :(得分:1)
如果MySecondControl.xaml
使用来自MyFirstControl.xaml
的资源,则将它们添加到generic.xaml的ResourceDictionary的顺序无关紧要。您需要在MyFirstControl.xaml
中冗余地包含MySecondControl.xaml
。 MySecondControl.xaml
应包含:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source='/MyControls;component/Themes/MyFirstControl.xaml'/>
</ResourceDictionary.MergedDictionaries>
<!-- ... Contents of MySecondControl.xaml -->
答案 2 :(得分:0)
我刚刚解决了这个问题。 ResourceDictionaries支持MergedDictionaries,但对于使用Generic.xaml的自定义模板化控件,Generic.xaml不支持MergedDictionaries。所以有两种选择:(1)你要么将所有模板都堆成Generic.xaml;或者(2)创建YourOwnDictionary.xaml,将所有单独的词典合并到YourOwnDictionary.xaml中,并从usercontrols和pages引用YourOwnDictionary.xaml。这似乎是早期Silverlight版本不支持合并字典的功能/错误。