gpt4 book ai didi

WPF : Extend Theme's style - StackOverflowException

转载 作者:行者123 更新时间:2023-12-04 12:06:00 25 4
gpt4 key购买 nike

除了 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>

它编译但我得到:

An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll


public Window1() {
InitializeComponent(); // <-- getting exception here
}

没有异常详细信息,因为:

{Cannot evaluate expression because the current thread is in a stack overflow state.}

最佳答案

这似乎是您的风格与 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>

关于WPF : Extend Theme's style - StackOverflowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/927251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com