CommunityToolkit.Maui.Options 允許開發者自訂 CommunityToolkit.Maui。 工具包的行為可能會因這些設定而有所不同。
呼叫 .UseMauiCommunityToolkit() 時,應在啟動時指派 Options:
var builder = MauiApp.CreateBuilder();
builder.UseMauiCommunityToolkit(options =>
{
options.SetShouldSuppressExceptionsInConverters(false);
options.SetShouldSuppressExceptionsInBehaviors(false);
options.SetShouldSuppressExceptionsInAnimations(false);
})
SetShouldSuppressExceptionsInConverters
當設定為 true 時,若實作 CommunityToolkit.Maui.Converters.BaseConverter 的轉換器拋出 Exception,則會捕捉到 Exception、透過 Debug.WriteLine() 記錄,並返回預先決定的預設值。
預設值為 false。
Example
此選項在呼叫 .UseMauiCommunityToolkit()時啟用:
var builder = MauiApp.CreateBuilder();
builder.UseMauiCommunityToolkit(options =>
{
options.SetShouldSuppressExceptionsInConverters(true);
})
預設回報值
當設為 true 時,若 Converter 拋出 Exception,則會回傳預設值。
包含兩個預設值:
public object? ICommunityToolkitValueConverter.DefaultConvertReturnValue { get; set; }-
Default value returned when Convert(object? value, Type targetType, object? parameter, CultureInfo? culture)投擲Exception
-
public object ICommunityToolkitValueConverter.DefaultConvertBackReturnValue { get; set; }-
Default value returned when ConvertBack(object? value, Type targetType, object? parameter, CultureInfo? culture)拋出一個Exception
-
以下是一個設定預設值的 BoolToObjectConverter範例:
XAML
<ContentPage.Resources>
<SolidColorBrush x:Key="TrueColorBrush">Green</SolidColorBrush>
<SolidColorBrush x:Key="FalseColorBrush">Red</SolidColorBrush>
<toolkit:BoolToObjectConverter x:Key="BoolToColorBrushConverter"
TrueObject="{StaticResource TrueColorBrush}"
FalseObject="{StaticResource FalseColorBrush}"
DefaultConvertReturnValue="{StaticResource FalseColorBrush}"
DefaultConvertBackReturnValue="False"/>
</ContentPage.Resources>
C#
var boolToColorBrushConverter = new BoolToObjectConverter
{
TrueObject = new SolidColorBrush(Colors.Green),
FalseObject = new SolidColorBrush(Colors.Red),
DefaultConvertReturnValue = new SolidColorBrush(Colors.Red),
DefaultConvertBackReturnValue = false
};
設定是否在動畫中抑制例外
當設為 true 時,若實作 CommunityToolkit.Maui.Behaviors.AnimationBehavior 的 Animation 拋出 Exception,則會攔截該 Exception,並透過 Debug.WriteLine() 記錄。
預設值為 false。
Example
此選項在呼叫 .UseMauiCommunityToolkit()時啟用:
var builder = MauiApp.CreateBuilder();
builder.UseMauiCommunityToolkit(options =>
{
options.SetShouldSuppressExceptionsInAnimations(true);
})
SetShouldSuppressExceptionsInBehaviors
當設為 true 時,若實作 CommunityToolkit.Maui.Behaviors.BaseBehavior 的 Behavior 擲回 Exception,則會攔截 Exception,並透過 Debug.WriteLine() 進行記錄。
預設值為 false。
Example
此選項在呼叫 .UseMauiCommunityToolkit()時啟用:
var builder = MauiApp.CreateBuilder();
builder.UseMauiCommunityToolkit(options =>
{
options.SetShouldSuppressExceptionsInBehaviors(true);
})