Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit f9820f7

Browse files
authored
Implemented LazyView (#796)
* Created LazyView and implemented it on TabView * Added Preserve attribute * Update LazyTestView.xaml
1 parent 5dd6a85 commit f9820f7

File tree

12 files changed

+240
-1
lines changed

12 files changed

+240
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<pages:BasePage
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.LazyTabPage"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages.Views.TabView"
7+
xmlns:pages="clr-namespace:Xamarin.CommunityToolkit.Sample.Pages"
8+
xmlns:vm="clr-namespace:Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs"
9+
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
10+
<pages:BasePage.Content>
11+
<Grid>
12+
<xct:TabView
13+
TabContentBackgroundColor="Yellow"
14+
TabIndicatorColor="Yellow"
15+
TabStripBackgroundColor="Blue"
16+
TabStripHeight="60"
17+
TabStripPlacement="Bottom">
18+
<xct:TabViewItem
19+
FontSize="12"
20+
Icon="triangle.png"
21+
Text="Tab Normal 1"
22+
TextColor="White"
23+
TextColorSelected="Yellow">
24+
<local:NormalTestView />
25+
</xct:TabViewItem>
26+
<xct:TabViewItem
27+
FontSize="12"
28+
Icon="circle.png"
29+
Text="Tab Lazy 2"
30+
TextColor="White"
31+
TextColorSelected="Yellow">
32+
<xct:LazyView
33+
x:TypeArguments="local:LazyTestView"
34+
BindingContext="{x:Static vm:LazyTestViewModel.Current}"
35+
IsLoaded="{Binding Loaded}" />
36+
</xct:TabViewItem>
37+
<xct:TabViewItem
38+
FontSize="12"
39+
Icon="triangle.png"
40+
Text="Tab Normal 3"
41+
TextColor="White"
42+
TextColorSelected="Yellow">
43+
<local:NormalTestView />
44+
</xct:TabViewItem>
45+
46+
<xct:TabViewItem
47+
FontSize="12"
48+
Icon="triangle.png"
49+
Text="Tab Lazy 4"
50+
TextColor="White"
51+
TextColorSelected="Yellow">
52+
<xct:LazyView x:TypeArguments="local:LazyTestView" BindingContext="{x:Static vm:LazyTestViewModel.Current}" />
53+
</xct:TabViewItem>
54+
</xct:TabView>
55+
</Grid>
56+
</pages:BasePage.Content>
57+
</pages:BasePage>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
2+
{
3+
public partial class LazyTabPage
4+
{
5+
public LazyTabPage() => InitializeComponent();
6+
}
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ContentView
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.LazyTestView"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:xct="http://xamarin.com/schemas/2020/toolkit">
7+
<ContentView.Content>
8+
<Grid RowDefinitions="auto, *">
9+
<Label
10+
FontSize="Title"
11+
HorizontalTextAlignment="Center"
12+
Text="{Binding Title}"
13+
TextColor="Black" />
14+
<xct:UniformGrid x:Name="uniformGrid" Grid.Row="1" />
15+
</Grid>
16+
</ContentView.Content>
17+
</ContentView>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs;
2+
using Xamarin.Forms;
3+
4+
namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
5+
{
6+
public partial class LazyTestView : ContentView
7+
{
8+
public LazyTestView()
9+
{
10+
InitializeComponent();
11+
12+
Build();
13+
NormalTestViewModel.Current.LoadedViews += "LazyView Loaded \n";
14+
}
15+
16+
void Build()
17+
{
18+
for (var i = 0; i < 117; i++)
19+
{
20+
var box = new BoxView
21+
{
22+
BackgroundColor = i % 2 == 0 ? Color.Blue : Color.Fuchsia
23+
};
24+
25+
uniformGrid.Children.Add(box);
26+
}
27+
}
28+
}
29+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ContentView
3+
x:Class="Xamarin.CommunityToolkit.Sample.Pages.Views.TabView.NormalTestView"
4+
xmlns="http://xamarin.com/schemas/2014/forms"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
6+
<ContentView.Content>
7+
<StackLayout>
8+
<Label Text="{Binding LoadedViews}" />
9+
</StackLayout>
10+
</ContentView.Content>
11+
</ContentView>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs;
2+
using Xamarin.Forms;
3+
4+
namespace Xamarin.CommunityToolkit.Sample.Pages.Views.TabView
5+
{
6+
public partial class NormalTestView : ContentView
7+
{
8+
public NormalTestView()
9+
{
10+
InitializeComponent();
11+
BindingContext = NormalTestViewModel.Current;
12+
13+
NormalTestViewModel.Current.LoadedViews += "NormalTestLoaded \n";
14+
}
15+
}
16+
}

samples/XCT.Sample/ViewModels/Views/TabViewViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public class TabViewViewModel : BaseGalleryViewModel
3939
"Customize the tabs width"),
4040

4141
new SectionModel(typeof(NoContentPage), "Tab without Content",
42-
"Only the TabStrip is visible")
42+
"Only the TabStrip is visible"),
43+
44+
new SectionModel(typeof(LazyTabPage), "LazyLoadingTab",
45+
"See how you can implement LazyViews that are loaded just when you navigate to them"),
4346
};
4447
}
4548
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Xamarin.CommunityToolkit.ObjectModel;
2+
3+
namespace Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs
4+
{
5+
sealed class LazyTestViewModel : ObservableObject
6+
{
7+
public static LazyTestViewModel Current { get; } = new LazyTestViewModel();
8+
9+
string title;
10+
11+
public string Title
12+
{
13+
get => title;
14+
set => SetProperty(ref title, value);
15+
}
16+
17+
bool loaded;
18+
19+
public bool Loaded
20+
{
21+
get => loaded;
22+
set => SetProperty(ref loaded, value);
23+
}
24+
25+
public LazyTestViewModel() => Title = "Lazy Tab Sample";
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Xamarin.CommunityToolkit.ObjectModel;
2+
3+
namespace Xamarin.CommunityToolkit.Sample.ViewModels.Views.Tabs
4+
{
5+
sealed class NormalTestViewModel : ObservableObject
6+
{
7+
public static NormalTestViewModel Current { get; } = new NormalTestViewModel();
8+
9+
string loadedViews;
10+
11+
public string LoadedViews
12+
{
13+
get => loadedViews;
14+
set => SetProperty(ref loadedViews, value);
15+
}
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using Xamarin.Forms;
3+
using Xamarin.Forms.Internals;
4+
5+
namespace Xamarin.CommunityToolkit.UI.Views
6+
{
7+
[Preserve(Conditional =true)]
8+
public abstract class BaseLazyView : ContentView, IDisposable
9+
{
10+
internal static readonly BindablePropertyKey IsLoadedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoaded), typeof(bool), typeof(BaseLazyView), default);
11+
12+
public static readonly BindableProperty IsLoadedProperty = IsLoadedPropertyKey.BindableProperty;
13+
14+
public bool IsLoaded => (bool)GetValue(IsLoadedProperty);
15+
16+
internal void SetIsLoaded(bool isLoaded) => SetValue(IsLoadedPropertyKey, isLoaded);
17+
18+
public abstract void LoadView();
19+
20+
public void Dispose()
21+
{
22+
if (Content is IDisposable disposable)
23+
disposable.Dispose();
24+
}
25+
26+
protected override void OnBindingContextChanged()
27+
{
28+
if (Content != null && !(Content is ActivityIndicator))
29+
Content.BindingContext = BindingContext;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)