LazyView

El LazyView control permite retrasar la inicialización de .View Debes proporcionar el tipo del View que quieres representar, usando el x:TypeArguments atributo de espacio de nombres XAML y controlar su inicialización mediante el LoadViewAsync método . La HasLazyViewLoaded propiedad se puede examinar para determinar cuándo se carga .LazyView

Syntax

Incluir el espacio de nombres XAML

Para usar el kit de herramientas en XAML, es necesario agregar el siguiente xmlns a la página o vista:

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

Por lo tanto, lo siguiente:

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

Se modificaría para incluir el xmlns de la siguiente manera:

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

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

En el código subyacente, puede hacer que la vista se cargue llamando al LoadViewAsync método .

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

Propiedades

Propiedad Tipo Descripción
HasLazyViewLoaded bool Obtiene el estado cargado de .LazyView

Methods

Propiedad Tipo de valor devuelto Descripción
LoadViewAsync ValueTask Inicialice .View

Ejemplos

Puede encontrar un ejemplo de esta característica en acción en la aplicación de ejemplo .NET MAUI Community Toolkit.

API

Puede encontrar el código fuente de LazyView en el repositorio .NET MAUI Community Toolkit GitHub.