-
DescriptionFollowing this example into XCT.Sample Each time we add a BindingContext to a LazyView, the viewmodel is created at the start, without Lazy mode Steps to Reproduce
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
@auriou in that case the VM is created because it's a static value, as you can see here and it's the used in the NormalTabs. So for this sample, the VM will be created even if the LazyView isn't loaded, because it's the same view model used by Non-LazyView. And also I'm referencing a static property, so the VM will be instantiated ASAP. Hope that makes sense for you |
Beta Was this translation helpful? Give feedback.
-
Ok, I see, so we must avoid using the BindingContext property in the Xaml for LazyView, as we can bind BindingContext only on static code |
Beta Was this translation helpful? Give feedback.
-
I solved my problem by implementing a class that inherits from LazyView public class CustomLazyView<TView> : LazyView<TView> where TView : View, new()
{
public static readonly BindableProperty ViewModelProperty =
BindableProperty.Create(nameof(ViewModel), typeof(Type),
typeof(CustomLazyView<TView>), default);
public Type ViewModel
{
get => (Type)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
public override ValueTask LoadViewAsync()
{
if (ViewModel != null)
BindingContext = Resolver.Resolve(ViewModel);
return base.LoadViewAsync();
}
} <xct:TabViewItem Style="{StaticResource TabViewItemShell}" Text="{x:Static styles:IconsMaterial.Account}">
<controls:CustomLazyView x:TypeArguments="views:SettingPage" ViewModel="{x:Type viewModels:SettingViewModel}" />
</xct:TabViewItem>
|
Beta Was this translation helpful? Give feedback.
I solved my problem by implementing a class that inherits from LazyView