【发布时间】:2017-11-17 10:41:54
【问题描述】:
问题
我想对项目 B 中定义的控件进行样式设置,但在项目 A 的页面上使用项目 A 中的样式。我决定通过使用包含在 MyCtrl.xaml 中使用的键的样式字典定义合并字典来实现。
我认为 MainPage 中使用的 MyCtrl 会在 XAML 树中找到正确的样式,如果它在自己的 xaml(更广泛的范围)中找不到它,但我得到了找不到名称/键的异常。是我做错了什么,还是这个想法不正确,我应该找到其他解决方案?
结构如下:
我有 2 个项目:
- 项目 A - 具有 MainPage 页面和所有样式/资源的主应用程序。
- 项目 B - 包含用户控件
项目 A 引用项目 B。
(项目 A)MainPage.xaml
<Page
xmlns:ext="using:ProjB.Ctrls">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///SomeDir/MyCtrlStyles.xaml" />
<ResourceDictionary Source="ms-appx:///SomeDir/MainPageStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</Page.Resources>
…
<ext: MyCtrl x:Id="myCtrlA" />
…
</Page>
(项目 A)MainPageStyles.xaml
<ResourceDictionary>
<Style x:Key="StyleAAA" />
</ResourceDictionary>
(项目 A)MainPageStyles.xaml
<ResourceDictionary>
<Style x:Key="StyleBBB" BasedOn="{StaticResource BodyTextBlockStyle}" TargetType="TextBlock">
<Setter Property="Margin" Value="24 13 12 0" />
</Style>
</ResourceDictionary>
(项目 B)MyCtrl.xaml
<UserControl>
<TextBlock x:Name="txtA" Style="{StaticResource StyleBBB}" Text="Text A" />
</ UserControl>
【问题讨论】:
标签: xaml uwp resourcedictionary