Can I share UWP viewmodels with xamarin iOS? - xamarin

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.

Related

How do you mix Xamarin.Forms with Xamarin.Native using MvvmCross?

According to MvvmCross 5.3 documentation you can mix Xamarin.Forms and Xamarin.Native:
"With MvvmCross 5.3, you will now be able to mix and match Native Xamarin views with Xamarin.Forms pages using ViewModel navigation!"
My scenario is the solution contains Xamarin.Android and Xamarin.iOS projects which rely quite heavily on MvvmCross. Going forward I would like to be able to create new views/pages using Xamarin.Forms. I'm assuming this is what is meant by "mix and match".
Other than the above statement I can't find any documentation or examples on how to do this.
I tried extending the MvvmCross TipCal app to show a new Xamarin.Forms view/page without any luck.
Can someone explain or provide an example of how to mix and match Xamarin.Forms with Native Xamarin using MvvmCross?
I think if you truly want to mix forms/native you need to either create a custom view presenter or trick the existing view presenter to "embed" a forms page into a viewcontroller/fragment/activity.
I have created a helper library as a proof of concept to embed Xamarin.Forms content pages into fragments/activities/viewControllers but could use some help ironing out some bugs with Android. It seems to work fine for iOS.
Let's take iOS as an example.
This approach has you create you content page as normal, but also create a wrapper viewController. You embed the content page in the viewController and when you navigate, you navigate to the viewController wrapper instead of the content page.
https://github.com/dornerworks/Mvx.Forms.PageWrapper.iOS
https://github.com/dornerworks/Mvx.Forms.PageWrapper.Android

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.

Xamarin - mixing Forms and Storyboards

Is it possible to mix iOS Storyboards and Xamarin Forms within one application? I have a bunch of views that are easier to be created in Forms, but some which are heavily customized so we would need to create some of them in native code.
I would like to use Storyboards to create the native parts, but can't seem to find a way to navigate from a Forms page to a Storyboard and vice versa.
I don't mind doing it from code, just need to know the direction to look into and if it's even possible.
You can create native views using the concept of Custom Renderers (see links below). The idea is that you create a Xamarin Forms Control that's shared between all platforms and which old common properties (like colors, general data etc), and do the native rendering on the iOS/Android/WP projects.
So, for your storyboard, you can create it usign Xamarin.iOS, and render using a Custom Renderer. The link posted by #GSerg in the comments have some information and examples, but you can take a closer look at the oficial documentation as well:
Introduction to Custom Renderers
Customizing Controls on Each Platform
Customizing Control Rendering in Xamarin.Forms (video)
Also, for more real world examples you can take a look at the Xamarin Forms XLabs project.
Thanks to Rafael Steil's answer. I looked at the links and a few more samples.
Notably:
Custom Renderer Map
Using Xamarin Forms alongside Storyboard
And I created a sample project to show the back-and-forth navigation between Xamarin Forms and pages created in Storyboards. You can find it over here:
Xamarin Forms Mixed with Native

How to use MVVM Light DispatcherHelper with Xamarin.Forms

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

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