WPF:扩展主题的样式 - StackOverflowException

发布于 2024-07-22 17:16:47 字数 1270 浏览 5 评论 0原文

除了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖 2024-07-29 17:16:47

这似乎是您的样式与PresentationFramework.Royale 中定义的样式之间的循环引用。 解决方法是将资源放置在不同级别:

<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>
    </ResourceDictionary>
</Window.Resources>
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </StackPanel.Resources>
    <Button Content="Button A"/>
</StackPanel>
</Window>

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:

<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>
    </ResourceDictionary>
</Window.Resources>
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </StackPanel.Resources>
    <Button Content="Button A"/>
</StackPanel>
</Window>
笑叹一世浮沉 2024-07-29 17:16:47

我会删除 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!

緦唸λ蓇 2024-07-29 17:16:47

请参阅这个问题我对另一个解决方案的回答,该解决方案不需要您在每个窗口中指定资源字典,并允许您动态解析 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文