Why is {x:Null} no longer a valid value in a Style

2019-02-24 03:40发布

A follow up to this question, why is {x:Null} no longer a valid option for Setter.Value?

Put this code in a resource dictionary in your UWP app:

<Style x:Key="MyList"
        TargetType="ListView">
    <Setter Property="Transitions" 
            Value="{x:Null}"/>
</Style>

And use it in any ListView:

<ListView Style="{StaticResource MyList}"/>

Your app will crash. A nasty crash as well, the kind that takes down the app without breaking properly in your debugger.

Also, the designer complains with a Catastrophic failure:

enter image description here

I know that setting values to {x:Null} is valid. If you set the <ListView Transitions="{x:Null}"/> directly, then it works fine... as expected.

So... what happened with style setters? ... Care to explain, Microsoft? Is there at least an alternate method?

2条回答
家丑人穷心不美
2楼-- · 2019-02-24 04:22

It is really a weird behavior and overall because as you explained setting directly to null works and with the style does not. The only alternative I found is just set a clear transitioncollection:

<Page.Resources>
<Style x:Key="MyList" TargetType="ListView">
    <Setter Property="Transitions" >
        <Setter.Value>
            <TransitionCollection></TransitionCollection>
        </Setter.Value>
    </Setter>
    <Setter Property="DataContext" Value="{x:Null}"/>
</Style>

I set the example of the DataContext that can be set to null, it says the same error but compiles and works.

I know it is not the best solution but it is a workaround to have a clear transitioncollection.

查看更多
聊天终结者
3楼-- · 2019-02-24 04:23

in this quiestion null-value-inside-a-template i've answered and you can find more answers

<Color x:Key="BgNull"></Color>
<Style x:Key="L2" TargetType="TextBox">
  <Setter Property="Background" Value="{StaticResource BgNull}"/>
</Style>
查看更多
登录 后发表回答