WinUI 應用程式可讓您使用函式作為標記延伸模組中 {x:Bind} 資料系結路徑的分葉步驟。 此功能簡化了值轉換,並使綁定能夠依賴多個參數,使您的應用程式更加動態和高效。
小提示
如需在應用程式中使用 {x:Bind} 進行數據系結的一般資訊,或是想進行 {x:Bind} 與 {Binding}的全面比較,請參閱 的「深入數據系結」 和 的「{x:Bind} 標記延伸」。
在下列範例中,專案的背景和前景會繫結至根據 color 參數執行轉換的函式。
<DataTemplate x:DataType="local:ColorEntry">
<Grid Background="{x:Bind local:ColorEntry.Brushify(Color), Mode=OneWay}" Width="240">
<TextBlock Text="{x:Bind ColorName}" Foreground="{x:Bind TextColor(Color)}" Margin="10,5" />
</Grid>
</DataTemplate>
public class ColorEntry
{
public string ColorName { get; set; }
public Color Color { get; set; }
public static SolidColorBrush Brushify(Color c)
{
return new SolidColorBrush(c);
}
public SolidColorBrush TextColor(Color c)
{
return new SolidColorBrush(((c.R * 0.299 + c.G * 0.587 + c.B * 0.114) > 150) ? Colors.Black : Colors.White);
}
}
XAML 屬性使用方式
<object property="{x:Bind pathToFunction.FunctionName(functionParameter1, functionParameter2, ...), bindingProperties}" ... />
函式的路徑
指定 函數的路徑 ,就像其他屬性路徑一樣。 路徑可以包含 點 (.)、 索引子或 轉換 ,以找出函式。
使用 XMLNamespace:ClassName.MethodName 語法來指定靜態函數。 例如,使用下列語法系結至程式碼後置中的靜態函式。
<Window
xmlns:local="using:MyNamespace">
...
<StackPanel>
<TextBlock x:Name="BigTextBlock" FontSize="20" Text="Big text" />
<TextBlock FontSize="{x:Bind local:MyHelpers.Half(BigTextBlock.FontSize)}"
Text="Small text" />
</StackPanel>
</Window>
namespace MyNamespace
{
static public class MyHelpers
{
public static double Half(double value) => value / 2.0;
}
}
您也可以直接在標記中使用系統函式來完成簡單的案例,例如日期格式設定、文字格式設定、文字串連等等。 例如:
<Window
xmlns:sys="using:System"
xmlns:local="using:MyNamespace">
...
<CalendarDatePicker Date="{x:Bind sys:DateTime.Parse(TextBlock1.Text)}" />
<TextBlock Text="{x:Bind sys:String.Format('{0} is now available in {1}', local:MyPage.personName, local:MyPage.location)}" />
</Window>
如果您將模式設定為 OneWay 或 TwoWay,則函數路徑支援變更偵測。 如果這些物件變更,繫結引擎會重新評估繫結。
您要繫結的函式需要:
- 可訪問程式碼和中繼資料—在 C# 中,方法可以是內部或私人,但在 C++ 中,方法必須是公用 WinRT 方法。
- 支援根據參數數量而不是類型進行重載,並且它會嘗試將第一個重載與該數量的參數進行匹配
- 具有符合傳入資料的引數類型 – 繫結引擎不會執行縮小轉換
- 屬性的回傳類型應符合使用繫結的屬性類型
繫結引擎會回應使用函式名稱引發的屬性變更通知,並視需要重新評估繫結。 例如:
<DataTemplate x:DataType="local:Person">
<StackPanel>
<TextBlock Text="{x:Bind FullName}" />
<Image Source="{x:Bind IconToBitmap(Icon, CancellationToken), Mode=OneWay}" />
</StackPanel>
</DataTemplate>
public class Person : INotifyPropertyChanged
{
//Implementation for an Icon property and a CancellationToken property with PropertyChanged notifications
...
//IconToBitmap function is essentially a multi binding converter between several options.
public Uri IconToBitmap (Uri icon, Uri cancellationToken)
{
var foo = new Uri(...);
if (isCancelled)
{
foo = cancellationToken;
}
else
{
if (fullName.Contains("Sr"))
{
//pass a different Uri back
foo = new Uri(...);
}
else
{
foo = icon;
}
}
return foo;
}
//Ensure FullName property handles change notification on itself as well as IconToBitmap since the function uses it
public string FullName
{
get { return fullName; }
set
{
fullName = value;
OnPropertyChanged();
OnPropertyChanged("IconToBitmap");
//this ensures Image.Source binding re-evaluates when FullName changes in addition to Icon and CancellationToken
}
}
}
小提示
使用 x:Bind 中的函式來實現與 WPF 中透過轉換器和多重繫結所支援的相同案例。
函式參數
指定多個以逗號 (,) 分隔的函數引數。
- 繫結路徑 — 使用與直接繫結至該物件相同的語法。
- 如果您將模式設定為 OneWay 或 TwoWay,繫結會偵測變更,並在物件變更時重新評估。
- 以引號括住的常數字串 — 包含引號以將其指定為字串。 使用帽子 (^) 來轉義字串中的引號。
- 常數 — 例如,-123.456。
- 布林值 — 指定為 “x:True” 或 “x:False”。
小提示
TargetNullValue 會套用至函式呼叫的結果,而不是任何系結引數。
雙向功能繫結
在雙向繫結的情況中,您必須為反向繫結指定第二個函式。 使用此 BindBack 函式的繫結屬性。 在以下範例中,函數接受一個參數,即需要推送回模型的值。
<TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />