LazyView

Le LazyView contrôle vous permet de retarder l’initialisation d’un View. Vous devez fournir le type du View que vous souhaitez restituer, à l’aide de l’attribut d’espace de noms XAML x:TypeArguments, et gérer son initialisation à l’aide de la méthode LoadViewAsync. La HasLazyViewLoaded propriété peut être examinée pour déterminer quand le LazyView chargement est effectué.

Syntax

Y compris l’espace de noms XAML

Pour utiliser le kit de ressources dans XAML, le xmlns suivant doit être ajouté à votre page ou à votre affichage :

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

Par conséquent, ce qui suit :

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

Serait modifié pour inclure le xmlns de la manière suivante :

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

Utilisation de LazyView

<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="CommunityToolkit.Maui.Sample.Pages.Views.LazyViewPage"
    xmlns:local="clr-namespace:CommunityToolkit.Maui.Sample.Pages.Views.LazyView"
    Title="Lazy View">

    <StackLayout>
        <toolkit:LazyView x:Name="LazyUserAction" x:TypeArguments="local:LazyTestView" />
        <Button Text="Load View Now" Clicked="LoadLazyView_Clicked" />
    </StackLayout>

</ContentPage>

Dans votre code-behind, vous pouvez faire en sorte que la vue se charge en appelant la méthode LoadViewAsync.

async void LoadLazyView_Clicked(object sender, EventArgs e)
{
    await LazyUserAction.LoadViewAsync();
}

Propriétés

Propriété Type Description
HasLazyViewLoaded bool Retourne l’état de chargement du LazyView.

Méthodes

Propriété Type renvoyé Description
LoadViewAsync ValueTask Initialisez le View.

Exemples

Vous trouverez un exemple de cette fonctionnalité en action dans l’exemple d’application .NET MAUI Community Toolkit.

API

Vous trouverez le code source de LazyView sur le dépôt .NET MAUI Community Toolkit GitHub.