WPF:扩展主题的样式 - StackOverflowException
除了 Royale 主题风格之外,我希望每个按钮都有 5 点边距。
Window1.xaml:
<Window x:Class="_styles.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Content="Button A"/>
<Button Content="Button B"/>
</StackPanel>
</Window>
它可以编译,但我得到:
PresentationFramework.dll 中发生“System.StackOverflowException”类型的未处理异常
public Window1() {
InitializeComponent(); // <-- getting exception here
}
没有异常详细信息,因为:
{无法计算表达式,因为当前线程处于堆栈溢出状态。}
I want every button to have 5 points margin, in addition to Royale theme style.
Window1.xaml:
<Window x:Class="_styles.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Content="Button A"/>
<Button Content="Button B"/>
</StackPanel>
</Window>
It compiles but I get:
An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll
public Window1() {
InitializeComponent(); // <-- getting exception here
}
There are no exception details because:
{Cannot evaluate expression because the current thread is in a stack overflow state.}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是您的样式与PresentationFramework.Royale 中定义的样式之间的循环引用。 解决方法是将资源放置在不同级别:
This seems to be a circular reference between your style and the one defined in PresentationFramework.Royale. A workaroud would be to place resources at different levels:
我会删除 BasedOn 属性 - 这是没有必要的。 这样想,合并 Royale 主题将应用按钮主题,而您只想更改边距 - 样式本质上是相加的,因此它将结合 Royale 主题和您自己的按钮主题不指定 BasedOn 属性 - 这有意义吗?
干杯!
I would remove the BasedOn attribute - it's not necessary. Think of it this way, merging the Royale theme will apply the button theme, and you just want to change the margin - styles are additive in nature, so it will combine the Royale theme and your own button theme without specifying the BasedOn attribute - does that make sense?
Cheers!
请参阅这个问题和我对另一个解决方案的回答,该解决方案不需要您在每个窗口中指定资源字典,并允许您动态解析 BasedOn 样式。
Please see this question and my answer for another solution that doesn't require you to specify a resource dictionary in every window and allows you to resolve the BasedOn style dynamically.