Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind ViewModel to XAML View in Prism.Forms

Tags:

In Xamarin.Forms it is possible to specify the ViewModel to be used with a XAML page within the markup via the <ContentPage.BindingContext> tag. The only advantage to doing this that I can see - vs. Prism.Forms' much more flexible ways of tying Views to ViewModels - is that then I get IntelliSense in the XAML about the VM properties/commands when adding my {Binding} attribute values.

Again, Prism.Forms has better ways to bind views and ViewModels, but when I use them, I usually don't get the IntelliSense in the XAML page - which would actually be quite helpful with a complex View/ViewModel.

I don't see any reason that I can't use the <ContentPage.BindingContext> tag in Prism.Forms scenarios. But that feels like I am "breaking" the way that Prism wants to do View-to-ViewModel bindings.

Also, I am worried that my ViewModel will be instantiated twice unnecessarily, once when Xamarin.Forms creates it; and once when Prism does its binding. Not sure if that is correct... Also, I can see a scenario where I want to bind my view to different ViewModels at various times; a different VM class for Android vs. iOS, for example. Prism lets me do that, but I am worried that wouldn't work with the <ContentPage.BindingContext> tag present.

Is there a supported/recommended way to identify my ViewModel in my XAML for design-time purposes only (i.e. for intellisense) but have Prism's view-to-viewmodel binding logic happen at run-time? I think I want the <ContentPage.BindingContext> tag to be completely ignored except while I am editing the XAML in Visual Studio.

EDIT: My question above was incorrect in that the XAML View IntelliSense that I was expecting to see is not provided by Visual Studio, but by JetBrains ReSharper. The solution described below did add back the expected IntelliSense functionality, but only in a development environment where ReSharper was installed (or there may be other Visual Studio add-ins with this feature).

like image 957
Jeremy Ellis Avatar asked Mar 13 '17 18:03

Jeremy Ellis


1 Answers

While I can't say I have heard of any consensus about how to best handle this issue, I can at least give you an answer. The tooling from Microsoft/Xamarin is still very new as far as being a stable release, and unfortunately out of the box it does not seem to support dynamically loading in ViewModels. I've gone ahead and created an issue on GitHub for Prism that you can follow here.

In the mean time if you need to make the binding more strongly typed so that the Xamarin Forms Previewer can find the ViewModel then I would suggest setting AutoWire=false so Prism does not create the ViewModel a second time.

As painful as it is to admit, sometimes being on the cutting edge (which the Xaml Previewer still is), means making sacrifices and having to break rules and patterns to make things work. Hope that helps.

like image 130
Dan Siegel Avatar answered Sep 24 '22 11:09

Dan Siegel