DateTimeOffsetConverter

DateTimeOffsetConverter è un convertitore che consente agli utenti di convertire un oggetto DateTimeOffset in un oggetto DateTime. A volte un valore DateTime viene archiviato con l'offset nel backend per memorizzare il fuso orario in cui ha avuto origine un DateTime. I controlli come Microsoft.Maui.Controls.DatePicker funzionano solo con DateTime. Questo convertitore può essere usato in questi scenari.

Proprietà BaseConverter

Le proprietà seguenti vengono implementate nella classe base public abstract class BaseConverter:

Proprietà Descrizione
DefaultConvertReturnValue Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Le proprietà di ICommunityToolkitValueConverter

Nella public interface ICommunityToolkitValueConvertervengono implementate le proprietà seguenti:

Proprietà Type Descrizione
DefaultConvertReturnValue object? Valore predefinito da restituire quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) genera un Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.
DefaultConvertBackReturnValue object? Valore predefinito da restituire quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) genera un Exception. Questo valore viene usato quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters è impostato su true.

Syntax

XAML

Inclusione del namespace XAML

Per usare il toolkit in XAML, è necessario aggiungere le informazioni seguenti xmlns nella pagina o nella visualizzazione:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"

Di conseguenza:

<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>

Verrà modificato in modo da includere il xmlns come indicato di seguito:

<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>

Utilizzo di DateTimeOffsetConverter

Il DateTimeOffsetConverter può essere usato come segue in 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#

Il DateTimeOffsetConverter può essere usato come indicato di seguito in 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;
    }
}

Linguaggio di markup C#

Il pacchetto CommunityToolkit.Maui.Markup offre un modo molto più conciso per usare questo convertitore in C#.

using CommunityToolkit.Maui.Markup;

class DateTimeOffsetConverterPage : ContentPage
{
    public DateTimeOffsetConverterPage()
    {
        Content = new Label()
            .Bind(
                Label.TextProperty,
                static (ViewModel vm) => vm.MyValue,
                converter: new DateTimeOffsetConverter());
    }
}

Esempi

È possibile trovare un esempio di questo convertitore in azione nell'applicazione di esempio .NET MAUI Community Toolkit .

API

È possibile trovare il codice sorgente per DateTimeOffsetConverter nel repository .NET MAUI Community Toolkit GitHub.