简体   繁体   English

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

[英]What is the difference between FallbackValue and TargetNullValue in UWP and WinUI3?

can someone please tell me the exact difference between FallbackValue and TargetNullValue ?有人可以告诉我FallbackValueTargetNullValue之间的确切区别吗? I know they are quite similar, but I'd like to understand more about those edge usecases where you want to use either one of the two or maybe usecases where using both makes sense.我知道它们非常相似,但我想更多地了解那些你想使用两者之一的边缘用例,或者可能同时使用两者都有意义的用例。

Cheers!干杯!

Basically,基本上,

  • FallbackValue is set when the binding itself fails. FallbackValue在绑定本身失败时设置。
  • TargetNullValue is set when the source of the binding is null . TargetNullValue在绑定源为null时设置。

These are the results I got with WinUI 3.这些是我使用 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情况1
x:Bind , FallbackValue / TargetNullValue , wrong name x:Bind , FallbackValue / TargetNullValue , 错误的名字

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.您会收到编译错误,因为x:Bind在编译时检查源代码。

Case#2案例#2
x:Bind , FallbackValue , null source x:Bind , FallbackValue , null 源

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

The TextBlock shows nothing (empty). TextBlock不显示任何内容(空)。

Case#3案例#3
Binding , FallbackValue , wrong name BindingFallbackValue ,错误名称

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

The TextBlock shows the FallbackValue "Binding failed.". TextBlock显示FallbackValue “绑定失败。”。

Case#4案例#4
x:Bind , TargetNullValue , null source x:Bind , TargetNullValue , null 源

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

The TextBlock shows the TargetNullValue "Source is null.". TextBlock显示TargetNullValue “Source is null.”。

Case#5案例#5
Binding , TargetNullValue , null source BindingTargetNullValue ,null 源

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

The TextBlock shows nothing (empty). TextBlock不显示任何内容(空)。

Case#6案例#6
Binding , FallbackValue , TargetNullValue , null source BindingFallbackValueTargetNullValue ,null 源

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

The TextBlock shows the FallbackValue "Binding failed.". TextBlock显示FallbackValue “绑定失败。”。

Case#7案例#7
x:Bind , FallbackValue , TargetNullValue , null source x:Bind , FallbackValue , TargetNullValue , null 源

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.". TextBlock显示TargetNullValue “Source is null.”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM