有人能告诉我FallbackValue和TargetNullValue之间的确切区别吗?我知道它们非常相似,但我想了解更多关于那些边缘用例的信息,在这些用例中,您希望使用两个中的一个,或者可能是使用两个都有意义的用例。干杯!干杯!
FallbackValue
TargetNullValue
xnifntxz1#
Basically,
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".
TextBlock
Case#1
x:Bind , FallbackValue / TargetNullValue , wrong name
x:Bind
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
Binding
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
<TextBlock Text="{x:Bind TestText, TargetNullValue='Source is null!'}" />
The TextBlock shows the TargetNullValue "Source is null!".
Case#5
Binding , TargetNullValue , null source
<TextBlock Text="{Binding TestText, TargetNullValue='Source is null!'}" />
Case#6
Binding , FallbackValue , TargetNullValue , null source
<TextBlock Text="{Binding TestText, FallbackValue='Binding failed!', TargetNullValue='Source is null!'}" />
Case#7
x:Bind , FallbackValue , TargetNullValue , null source
<TextBlock Text="{x:Bind TestText, FallbackValue='Binding failed!', 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!'}" />
Case#9
x:Bind / Binding , FallbackValue , null ViewModel
<TextBlock Text="{x:Bind ViewModel.TestText, FallbackValue='Binding failed!'}" />
1条答案
按热度按时间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
The
TextBlock
shows "Binding succeeded".Case#1
x:Bind
,FallbackValue
/TargetNullValue
, wrong nameYou get a compile error because
x:Bind
checks the source at compile time.Case#2
x:Bind
,FallbackValue
, null sourceThe
TextBlock
shows nothing (empty).Case#3
Binding
,FallbackValue
, wrong nameThe
TextBlock
shows theFallbackValue
"Binding failed!".Case#4
x:Bind
,TargetNullValue
, null sourceThe
TextBlock
shows theTargetNullValue
"Source is null!".Case#5
Binding
,TargetNullValue
, null sourceThe
TextBlock
shows nothing (empty).Case#6
Binding
,FallbackValue
,TargetNullValue
, null sourceThe
TextBlock
shows theFallbackValue
"Binding failed!".Case#7
x:Bind
,FallbackValue
,TargetNullValue
, null sourceThe
TextBlock
shows theTargetNullValue
"Source is null!".Case#8
x:Bind
/Binding
,TargetNullValue
, null ViewModelThe
TextBlock
shows nothing (empty).Case#9
x:Bind
/Binding
,FallbackValue
, null ViewModelThe
TextBlock
shows theFallbackValue
"Binding failed!".