当我尝试设置通过 WindowStartupLocation
属性二传手
在的ResourceDictionary
,我收到了 XamlParseException
:
When I attempt to set the WindowStartupLocation
property through a Setter
within a ResourceDictionary
, I get a XamlParseException
:
设置属性System.Windows.Setter.Property'引发了异常。行号'x'和线的位置Y。
'Set property 'System.Windows.Setter.Property' threw an exception.' Line number 'x' and line position 'y'.
的内部异常是 ArgumentNullException
:
值不能为空。参数名:属性
Value cannot be null. Parameter name: property.
我的资源字典中的风格是:
My style within the resource dictionary is:
<Style TargetType="Window" x:Key="WindowStyle">
<Setter Property="SizeToContent" Value="WidthAndHeight" />
<Setter Property="ResizeMode" Value="CanMinimize" />
<Setter Property="WindowStartupLocation" Value="CenterOwner" />
</Style>
这个问题是不是与使用的ResourceDictionary
,因为当我删除 WindowStartupLocation
,其他两个性能( SizeToContent
和 ResizeMode
)设置为预期引用该风格的窗户:
The issue is not with the use of the ResourceDictionary
, since when I remove the WindowStartupLocation
, the other two properties (SizeToContent
and ResizeMode
) are set as expected on windows which reference the style:
<Window x:Class="WpfApplication1.MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{DynamicResource WindowStyle}">
<Window.Resources>
<ResourceDictionary Source="MyResourceDictionary.xaml" />
</Window.Resources>
</Window>
有没有人遇到过吗?它是WPF的一个bug /限制吗?
Has anyone encountered this? Is it a bug/limitation of WPF?
P.S。我知道,这个问题类似于Window从资源字典的启动位置,但没有足够的信息是在随后没有解决其他问题提供的。
P.S. I know that this question is similar to Window Startup Location from resource dictionary, but not enough information was provided in the other question which subsequently remained unsolved.
推荐答案
问题是,WindowStartupLocation不是一个DependencyProperty的,所以你不能将其设置在风格的引领者。展望ILSpy二传手电话
The problem is that WindowStartupLocation is not a DependencyProperty so you can't set it in the style setter. Looking in ILSpy the Setter calls
CheckValidProperty(DependencyProperty property)
和抛出一个NullArgumentException。
and throws a NullArgumentException.
正如WindowStartupLocation只是一个CLR属性,它不能以这种方式设置
As WindowStartupLocation is just a CLR property, it can't be set in this way.
编辑:
为了应对意见。你仍然可以使用的ResourceDictionary
:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="SizeToContent" Value="WidthAndHeight" />
<Setter Property="ResizeMode" Value="CanMinimize" />
</Style>
<WindowStartupLocation x:Key="WSL">CenterOwner</WindowStartupLocation>
</ResourceDictionary>
</Application.Resources>
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="{StaticResource WSL}"
Style="{StaticResource WindowStyle}" />
相关推荐
最新文章