DateTimeOffsetConverterは、ユーザーがDateTimeOffsetをDateTimeに変換できるコンバーターです。 場合によっては、 DateTime 値がバックエンドのオフセットと共に格納され、 DateTime が発生したタイムゾーンを格納できます。
Microsoft.Maui.Controls.DatePicker などのコントロールは、DateTimeでのみ機能します。 このコンバーターは、これらのシナリオで使用できます。
BaseConverter プロパティ
基底クラスでは、次のプロパティが実装 public abstract class BaseConverter。
| 財産 | Description |
|---|---|
DefaultConvertReturnValue |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
ICommunityToolkitValueConverter プロパティ
public interface ICommunityToolkitValueConverterでは、次のプロパティが実装されています。
| 財産 | タイプ | Description |
|---|---|---|
DefaultConvertReturnValue |
object? |
IValueConverter.Convert(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
DefaultConvertBackReturnValue |
object? |
IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) が Exception をスローしたときに返される既定値。 この値は、 CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters が true に設定されている場合に使用されます。 |
Syntax
XAML
XAML 名前空間を含める
XAML でこのツールキットを使用するには、次の xmlns をページまたはビューに追加する必要があります。
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
したがって、以下の通りです
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
</ContentPage>
次のように、xmlns を含むように変更されます。
<ContentPage
x:Class="CommunityToolkit.Maui.Sample.Pages.MyPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">
</ContentPage>
DateTimeOffsetConverter の使用
DateTimeOffsetConverterは、XAML で次のように使用できます。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<toolkit:DateTimeOffsetConverter x:Key="DateTimeOffsetConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout>
<Label Text="The DatePicker below is bound to a Property of type DateTimeOffset."
Margin="16"
HorizontalOptions="Center"
FontAttributes="Bold" />
<DatePicker Date="{Binding TheDate, Converter={StaticResource DateTimeOffsetConverter}}"
Margin="16"
HorizontalOptions="Center" />
<Label Text="{Binding TheDate}"
Margin="16"
HorizontalOptions="Center"
FontAttributes="Bold" />
</VerticalStackLayout>
</ContentPage>
C#
DateTimeOffsetConverterは、C# で次のように使用できます。
class DateTimeOffsetConverterPage : ContentPage
{
public DateTimeOffsetConverterPage()
{
var label = new Label();
label.SetBinding(
Label.TextProperty,
new Binding(
static (ViewModels vm) => vm.MyValue,
converter: new DateTimeOffsetConverter()));
Content = label;
}
}
C# マークアップ
この CommunityToolkit.Maui.Markup パッケージは、C# でこのコンバーターを使用するためのより簡潔な方法を提供します。
using CommunityToolkit.Maui.Markup;
class DateTimeOffsetConverterPage : ContentPage
{
public DateTimeOffsetConverterPage()
{
Content = new Label()
.Bind(
Label.TextProperty,
static (ViewModel vm) => vm.MyValue,
converter: new DateTimeOffsetConverter());
}
}
例示
このコンバーターの例は、 .NET MAUI Community Toolkit サンプル アプリケーションで動作しています。
API
DateTimeOffsetConverter のソース コードは、.NET MAUI Community Toolkit GitHub リポジトリで確認できます。
.NET MAUI Community Toolkit