Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LazyView.cs
1using System;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
7
9{
10 public class LazyView<T> : ContentView where T : View
11 {
12 public static readonly BindableProperty DelayMsProperty =
13 BindableProperty.Create(
14 nameof(DelayMs),
15 typeof(int),
16 typeof(LazyView<T>),
17 100);
18
19 public int DelayMs
20 {
21 get => (int)this.GetValue(DelayMsProperty);
22 set => this.SetValue(DelayMsProperty, value);
23 }
24
25 private bool isLoaded = false;
26
27 public LazyView()
28 {
29 // Start loading after appearing
30 this.Loaded += async (s, e) => await this.TryLoadAsync();
31 }
32
33 private async Task TryLoadAsync()
34 {
35 if (this.isLoaded) return;
36 this.isLoaded = true;
37
38 if (this.DelayMs > 0)
39 await Task.Delay(this.DelayMs);
40
41 T PageOrView = ServiceRef.Provider.GetRequiredService<T>();
42
43 if (PageOrView.BindingContext is null)
44 PageOrView.BindingContext = this.BindingContext;
45
46 // (Optional) If you want to support Page or View, you can check here
47 if (PageOrView is View v)
48 this.Content = v;
49 else
50 throw new InvalidOperationException($"{typeof(T).Name} is not a View.");
51 }
52 }
53}
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
Definition: ServiceRef.cs:48