WPF:扩展主题的样式 - StackOverflowException

THX*_*138 4 wpf themes styles

除了皇家主题风格,我希望每个按钮都有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>
Run Code Online (Sandbox Code Playgroud)

它编译但我得到:

PresentationFramework.dll中出现未处理的"System.StackOverflowException"类型异常

public Window1() {
    InitializeComponent(); // <-- getting exception here
}
Run Code Online (Sandbox Code Playgroud)

没有例外细节,因为:

{无法计算表达式,因为当前线程处于堆栈溢出状态.}

Sta*_*zev 5

这似乎是您的样式与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>
Run Code Online (Sandbox Code Playgroud)