How to use MVVM Light DispatcherHelper with Xamarin.Forms - xamarin

I have a Xamarin Forms application that was created using the standard Xamarin.Forms Shared solution template for Visual Studio.
The Android project uses a MainActivity class that derives from Xamarin.Forms.Platform.Android.FormsApplicationActivity.
Within this solution, I am hoping to use the DispatcherHelper component from the MVVM Light Toolkit.
However, whenever I attempt to use DispatcherHelper.CheckBeginInvokeOnUI() to marshall to the UI thread, the app throws an InvalidOperationException, as follows:
The DispatcherHelper cannot be called.
Make sure that your main Activity derives from ActivityBase.
Looking at the inheritance hierarchy of FormsApplicationActivity, I can see that it does not derive from ActivityBase, so this clearly explains the problem.
However, I wonder if there is an alternative way of using this DispatcherHelper that is compatible with Xamarin Forms or, alternatively, an alternative base class from which I can derive my MainActivity.
Many thanks for your suggestions,
Tim

Related

Can I share UWP viewmodels with xamarin iOS?

What is realistic amount of code share between Xamarin and UWP?
I have app which has following layers:
1. UWP platform specific apis
2. Messaging protocol based on these apis
3. Services
4. ViewModels
5. Xaml Views communicating with INotifyPropertyChanged and ICommand
I understand that obviously 1 (platform specific apis) an 5 (xaml views) cannot be shared. Layers 2 and 3 should be ok. What about ViewModels? I think it should be possible to achieve this using thin layer of viewModel adapter, is this realistic?
Also I am not planning to use xamarin.forms.
The answer depends on your application.
UWP has many specific APIs that as such don't work on Xamarin. They are not necessary related to views only but can be also related to viewmodel code. If your viewmodel is a separate .Net standard project then you can reuse it completely, but as it is not always the case with UWP apps you can probably reuse just most of the code.
The second part comes down to actually using this viewmodel in iOS/macOS project. As you don't want to use XAML (Xamarin.Forms), there is no data binding provided. Basically you have to do whole data binding manually. Some MVVM frameworks may help you in that, but if you ask me they should be called 'helpers', not frameworks.

Xamarin Forms MVVM Pattern best place to put repeated UI Code

We have a xamarin forms app using the MVVM pattern. We have repeated UI logic that gets run whenever any contentView is loaded in the application and just wondering where the best place for this will go. Currently in our legacy application it is written in every control which is incredibly frustrating since if it needs fixing then it needs to be fixed in every separate location. What is the best approach for this type of code.
An example of what I mean is that on initialise of every contentView it runs through security privileges of the current user and hides or shows UI controls on that ContentView depending on what the user is allowed to see. This occurs on every form in the system. This is a simple example but there is plenty.
Any ideas?
Why not put it in a parent class? Derive from ContentView, then have all your relevant controls derive from that. You can do this with pages too. All pages in my latest Xamarin Forms app derive from this:
public abstract class BaseContentPage<T> : ContentPage, IViewFor<T> where T : class, IViewModel

Xamarin Forms IoC containter + navigation service

Does Xamarin.Forms have a built-in IoC navigation service? I mean something like Prism, where you could register your routes.
If yes - where is the documentation?
If not - will Xamarin.Forms have a built-in navigation service in near future?
Also - if not - what would be the best MVVM fw for Xamarin.Android, Xamarin.iOS, Xamarin.WinXYZ and Xamarin.Forms? And why?
It seems to me that it comes down to battle between Prism and FreshMVVM - this brings me to my most important questions:
Which of these two is performing better? (Which one is faster?)
Which of these is more likely to lead the way of MVVM frameworks considering mobile development in the future?
No Xamarin Forms does not offer navigation like Prism. They had a goal of making the built in Navigation similar to Prism, but that has since disappeared from their roadmap. There is also no direct IoC concept built directly into Xamarin Forms.
If you are developing native UI's then Prism probably isn't for you as it is purpose built for Xamarin Forms. In that case I might say you should look at MvvmCross. It is battle tested in a lot of classic Xamarin apps.
If however you are developing with Xamarin Forms, then Prism would be the best to use. My opinion may be biased, but it's also the public opinion of many of those on the Xamarin team. Remember Prism was originally started by the Microsoft Patterns and Practices team. While Prism for Xamarin Forms was started after that, the foundation of what Prism is and how it works, helps keep developers developing using proper MVVM patterns.
Question 1: Yes, Xamarin.Forms have a built-in navigation service.
Question 2: Xamarin.Forms navigation
Edit 2 for question 1: No, there is no IoC Navigation built in Xamarin.Forms framework.
But if you want some IoC navigation maybe this a good one: Xamarin Forms - View Model First Navigation avoiding big frameworks like Prism, MVVMLight or MVVMCross. If not this frameworks works well for this purpose.

Adding Xamarin.Forms to an existing Xamarin app

I'm trying to add a Xamarin.Forms page to an existing Xamarin Android app.
I'm struggling to figure out how to mix native Xamarin Android pages and Xamarin.Forms pages.
All the articles and examples I have seen describe either Forms or Xamarin apps and not a hybrid of the two.
The best idea I have read about so far is to add a native stub and replace the content with Xamarin.Forms content but I'm not sure how to achieve this.
Update
I'm using MvvmCross so my android view is inherited from MvxActivity. I have a StackLayout containing views using Xamarin.Forms. How do I get XF to convert to android code so I can use with SetContentView?
When you create a new Xamarin.Forms project, for instance with a SharedProject approach, it will create the launcher project and initial class file that will be executed for you for each platform.
For instance on Android this is in MainActivity.cs. If you look inside this file you will notice that it inherits from AndroidActivity which in turn inherits from Activity, which is the typical class used on activities.
So you have two options available for you to create a mixed hybrid solution:-
1) Inherit from AndroidActivity in your platform-specifc stub-page should you want to display Xamarin.Forms content, or alternatively
2) Inherit from Activity, and do the same as usual.
If you want to display a Xamarin.Forms page from the stub-page you can then just use some boiler-plate code such as the following in the OnCreate method:-
SetPage({some class that returns a Xamarin.Forms.Page object, i.e. ContentPage});
You can then use the normal navigation that is specific to Android to either display a Xamarin.Forms page or a platform-specific Activity page.

Same UI for all platforms in Mvvmcross Xamarin

Hello All, Can i code one UI for all platforms using Mvvm Cross framework in Xamarin?
Like i code for a generic UI which can generated for different platforms Android,IOS,Windows.
I saw Xamarin Forms giving some thing like this , but what about MVVMCross.
So if i conclude in simple sencario , since xamarin forms uses Mvvm so its better to use xamarin forms instead of mvvmcross for one single Ui
No, Mvvmcross doesn't provide UI automagically. View is platform specific while models and viewmodels are platform independent.
With Forms all three are platform independent. To make this work Forms added another layer, called Renderer (which is platform specific). Out of the box there is a bunch of renderers, then there is 3rd party community (specially Forms Labs) and finally, you can make your own renderers or extend existing ones.

Resources