DateTimeOffsetConverter

O DateTimeOffsetConverter é um conversor que permite aos utilizadores converter a DateTimeOffset para um DateTime. Por vezes, um valor DateTime é armazenado com o deslocamento no backend para permitir armazenar o fuso horário em que um DateTime teve origem. Controlos como o Microsoft.Maui.Controls.DatePicker só funcionam com DateTime. Este conversor pode ser usado nessas situações.

Propriedades BaseConverter

As seguintes propriedades são implementadas na classe base, public abstract class BaseConverter:

Propriedade Descrição
DefaultConvertReturnValue Valor padrão a ser retornado quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true.
DefaultConvertBackReturnValue Valor padrão a ser retornado quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true.

Propriedades de ICommunityToolkitValueConverter

As seguintes propriedades são implementadas no public interface ICommunityToolkitValueConverter:

Propriedade Tipo Descrição
DefaultConvertReturnValue object? Valor padrão a ser retornado quando IValueConverter.Convert(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true.
DefaultConvertBackReturnValue object? Valor padrão a ser retornado quando IValueConverter.ConvertBack(object?, Type, object?, CultureInfo?) lança um Exception. Esse valor é usado quando CommunityToolkit.Maui.Options.ShouldSuppressExceptionsInConverters é definido como true.

Sintaxe

XAML

Incluindo o namespace XAML

Para usar o kit de ferramentas em XAML, a seguinte xmlns precisa ser adicionada à sua página ou vista.

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

Por conseguinte, o seguinte:

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

Seria modificado para incluir o xmlns da seguinte forma:

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

Usando o DateTimeOffsetConverter

O DateTimeOffsetConverter pode ser usado da seguinte forma em 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#

O DateTimeOffsetConverter pode ser usado da seguinte forma em 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;
    }
}

Marcação em C#

Nosso pacote CommunityToolkit.Maui.Markup fornece uma maneira muito mais concisa de usar esse conversor em C#.

using CommunityToolkit.Maui.Markup;

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

Exemplos

Você pode encontrar um exemplo desse conversor em ação no .NET MAUI Community Toolkit Sample Application.

API

Você pode encontrar o código-fonte para DateTimeOffsetConverter no repositório GitHub do .NET MAUI Community Toolkit.