Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

c# - Setting WindowStartupLocation from ResourceDictionary throws XamlParseException

When I attempt to set the WindowStartupLocation property through a Setter within a ResourceDictionary, I get a XamlParseException:

'Set property 'System.Windows.Setter.Property' threw an exception.' Line number 'x' and line position 'y'.

The inner exception is an 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>

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>

Has anyone encountered this? Is it a bug/limitation of WPF?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

and throws a NullArgumentException.

As WindowStartupLocation is just a CLR property, it can't be set in this way.

Edit: To respond to the comment. You can still use a 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}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...