XAML UWP和WinUI3中的FallbackValue和TargetNullValue之间有什么区别?

ubby3x7f  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(112)

有人能告诉我FallbackValueTargetNullValue之间的确切区别吗?我知道它们非常相似,但我想了解更多关于那些边缘用例的信息,在这些用例中,您希望使用两个中的一个,或者可能是使用两个都有意义的用例。
干杯!干杯!

xnifntxz

xnifntxz1#

Basically,

  • FallbackValue is set when the binding itself fails.
  • TargetNullValue is set when the source of the binding is null.

These are the results I got with WinUI 3.

Plain binding

public string TestText { get; set; } = "Binding succeeded!";
<TextBlock Text="{x:Bind TestText}" />
  OR
<TextBlock Text="{Binding TestText}" />

The TextBlock shows "Binding succeeded".

Case#1

x:Bind , FallbackValue / TargetNullValue , wrong name

public string? TestText { get; set; } = "Binding succeeded!";
<TextBlock Text="{x:Bind Test, FallbackValue='Binding failed!'}" />
  OR
<TextBlock Text="{x:Bind Test, TargetNullValue='Source is null!'}" />

You get a compile error because x:Bind checks the source at compile time.

Case#2

x:Bind , FallbackValue , null source

public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, FallbackValue='Binding failed!'}" />

The TextBlock shows nothing (empty).

Case#3

Binding , FallbackValue , wrong name

public string TestText { get; set; } = "Binding successed!";
<TextBlock Text="{Binding Test, FallbackValue='Binding failed!'}" />

The TextBlock shows the FallbackValue "Binding failed!".

Case#4

x:Bind , TargetNullValue , null source

public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, TargetNullValue='Source is null!'}" />

The TextBlock shows the TargetNullValue "Source is null!".

Case#5

Binding , TargetNullValue , null source

public string? TestText { get; set; } = null;
<TextBlock Text="{Binding TestText, TargetNullValue='Source is null!'}" />

The TextBlock shows nothing (empty).

Case#6

Binding , FallbackValue , TargetNullValue , null source

public string? TestText { get; set; } = null;
<TextBlock Text="{Binding TestText, FallbackValue='Binding failed!', TargetNullValue='Source is null!'}" />

The TextBlock shows the FallbackValue "Binding failed!".

Case#7

x:Bind , FallbackValue , TargetNullValue , null source

public string? TestText { get; set; } = null;
<TextBlock Text="{x:Bind TestText, FallbackValue='Binding failed!', TargetNullValue='Source is null!'}" />

The TextBlock shows the TargetNullValue "Source is null!".

Case#8

x:Bind / Binding , TargetNullValue , null ViewModel

public ViewModel? ViewModel { get; set; } = null;
<TextBlock Text="{x:Bind ViewModel.TestText, TargetNullValue='Source is null!'}" />

The TextBlock shows nothing (empty).

Case#9

x:Bind / Binding , FallbackValue , null ViewModel

public ViewModel? ViewModel { get; set; } = null;
<TextBlock Text="{x:Bind ViewModel.TestText, FallbackValue='Binding failed!'}" />

The TextBlock shows the FallbackValue "Binding failed!".

相关问题