question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] LazyView

See original GitHub issue

LazyView

Summary

The LazyView control allows you to delay the initialization of a View. You need to provide the type of the View that you want to be rendered, using the x:TypeArguments XAML namespace attribute, and handle its initialization using the LoadViewAsync method. The IsLoaded property can be examined to determine when the LazyView is loaded.

Note: This proposal is a to port the existing LazyView functionality from the Xamarin Community Toolkit

Motivation

LazyView is one of the views that exist in the Xamarin Community Toolkit that has been requested as a missing piece in the migration of some big apps from Xamarin to MAUI.

Detailed Design

Properties:

Property Type Description
IsLoaded bool Gets the loaded status of the LazyView.

Methods:

Method Return Type Description
LoadViewAsync ValueTask Initialize the View.
Dispose void Cleans up the View, if required.

BaseLazyView.shared.cs

public abstract class BaseLazyView : ContentView, IDisposable
{
  internal static readonly BindablePropertyKey IsLoadedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoaded), typeof(bool), typeof(BaseLazyView), default);
  public static readonly BindableProperty IsLoadedProperty = IsLoadedPropertyKey.BindableProperty;
  
  public bool IsLoaded => (bool)GetValue(IsLoadedProperty);
  
  protected void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);
  
  public abstract ValueTask LoadViewAsync();
  
  public void Dispose()
  {
    if (Content is IDisposable disposable)
    disposable.Dispose();
  }
  
  protected override void OnBindingContextChanged()
  {
    if (Content != null && !(Content is ActivityIndicator))
    Content.BindingContext = BindingContext;
  }
}

LazyView.shared.cs

public class LazyView<TView> : BaseLazyView where TView : View, new()
{
  public override ValueTask LoadViewAsync()
  {
    View view = new TView { BindingContext = BindingContext };
    
    Content = view;
    
    SetIsLoaded(true);
    return new ValueTask(Task.FromResult(true));
  }
}

Usage Syntax

XAML Usage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:Class="MyLittleApp.MainPage"
             xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.TabView">

     <StackLayout>

        <xct:LazyView x:TypeArguments="local:LazyTestView" IsLoaded = "{Binding IsLoaded}" />

    </StackLayout>

</ContentPage>

C# Usage

new StackLayout
{
  Children = 
  {
    new LazyView<Button>()
      .Bind(LazyView.IsLoadedProperty, nameof(ViewModel.IsLoaded))
  }
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
VladislavAntonyukcommented, Dec 7, 2022

Maybe we should enhance it to DelayedView: https://www.sharpnado.com/delayed-view/

1reaction
pictoscommented, Dec 6, 2022

@ChaplinMarchais please, ask him to comment here, that way we can assign this issue to your friend

Read more comments on GitHub >

github_iconTop Results From Across the Web

Xamarin Community Toolkit LazyView
This article explains how to use LazyView, which lets you delay the initialization of a View.
Read more >
Lazy View Lodge - Pigeon Forge
Enjoy beautiful views, several amenities, and a prime location when staying at "Lazy View Lodge." This 2 bedroom cabin close to Pigeon Forge...
Read more >
Tours & Tickets SA (Hazyview) - All You Need to Know ...
Established in 1998 and offering 100+ activities from mild to wild from reputable safari and adventure operators. Tours & Tickets consultants will offer...
Read more >
250 House final for Hazyview ideas
Jul 12, 2021 - Explore Yoli's board "house final for Hazyview" on Pinterest. ... Floor Plan Layout, New House Plans, Sims 4 House...
Read more >
"Lazy View Lodge" 2 Bedroom Cabin ...
Step inside your 2 bedroom cabin rental near Gatlinburg with a hot tub to find a living room with comfortable furniture, a gas...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found