Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

View Model instantiating multiple times

$
0
0

Hi,

I have read that this problem had been fixed but I am experiencing it with Forms.xaml v1.4.

Here is my setup:

the view model is referenced in the view xaml like so
BindingContext="{Binding Path=TheNextViewModel, Source={StaticResource ServiceLocator}}"
the navigation to this view is done like so
public async Task NavigateToAsync(ViewDescriptor viewDescriptor, bool animated)
{
var pageName = viewDescriptor.PageType.ToString();
var type = Type.GetType(string.Format(PathTemplate, pageName));
var page = (Page)Activator.CreateInstance(type);

   var viewModel = page.BindingContext as IViewModel;

   if (viewModel != null)
   {
       viewModel.ViewArgs = viewDescriptor.ViewArgs;
   }

   await this.Navigation.PushAsync(page, animated);

}
The problem is when the code hits this line
await this.Navigation.PushAsync(page, animated);

the view model is instantiated again.

why is this happening?

The problem with this bug (I think this is a bug), is that the viewargs that was passed to the first instance of the viewmodel is now no longer there. Since the new instances are no longer getting the viewargs from the view descriptor.

Can someone help me?

Seb


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>