[Proposal] LazyView
See original GitHub issueLazyView
- Proposed
- Prototype:
- Implementation
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Sample
- Documentation: https://github.com/MicrosoftDocs/CommunityToolkit/pull/245
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:
- Created 2 years ago
- Reactions:8
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe we should enhance it to DelayedView: https://www.sharpnado.com/delayed-view/
@ChaplinMarchais please, ask him to comment here, that way we can assign this issue to your friend