Using Xamarin.Forms with Native pages (Xamarin.iOS, UWP) - xamarin

I already have a solution which consist of Xamarin.iOS and UWP. We have multiple views (Pages) in both platforms. We are using MvvmCross, so we have a common code (PCL) which consists of Viewmodels, business logic etc. Also, we have separate views (Pages) for both Xamarin.iOS and UWP.
Now I want to add Xamarin.Forms support for native pages of both projects so that we have same views for both Xamarin.iOS and UWP. We need the support of navigation (from Native to Xamarin.Forms & vice versa). I am navigating through MvvmCross navigation service.
For instance,
I have FirstFormView in Xamarin.Forms project.
I have SecondFormView in Native project (can be Xamarin.iOS or UWP).
I have ThirdFormView in Xamarin.Froms project.
I have navigated to FirstFormView (which is staring page of my application)
Now I want to go to SecondFormView (which is in native project).
And from SecondFormView I need to go to ThirdFormView which is again in Xamarin.Forms
Is it possible to do it MvvmCross? If yes, then what steps should we follow to achieve our desired goal.
Thanks in advance.
Regards,

This is possible through the Forms presenters. Take a look at the playground: https://github.com/MvvmCross/MvvmCross/tree/develop/TestProjects/Playground/Playground.Forms.Droid
Here the views are all Forms, but 1 or 2 are in Native. By using the same solution you should be able to do this.

You already have MvvmCross in your PCL project, just need to add Xamarin Form pages (XAML) for every page in platform specific project (iOS, UWP). And custom renderers for every platform.
For example:
//in xamarin forms pcl project
namespace XFormProject
{
public class MyXFormPage : ContentPage project
{
}
}
// in ios project
namespace XFormProjecy.iOS
{
[assembly:ExportRenderer(typeof(XFormProject.MyXFormPage), typeof(XFormProject.iOS.MyXFormPageIOS))]
public class MyXFormPageIOS : PageRenderer // in iOS
{
// your ios native page code goes here.
}
}
Read more about Custom Rederers
Hope this helps :)

Related

What's a non app compat Xamarin application?

I am looking at this issue which is effecting me:
https://github.com/xamarin/Xamarin.Forms/issues/3087
It mentions that this is a problem with a
Steps to Reproduce
Just use a switch on a non app compat application
But what's a "non app compat application"
Also does anyone know any more about the history of this problem. Is it just for Switch or for many other elements. Which version of Android is causing it? Looks like a big problem for anyone using an Android custom renderer.
Here's an explanation from the docs:
Originally, the default Xamarin.Forms Android project used an older style of control renderering that was common prior to Android 5.0. Applications built using the template have FormsApplicationActivity as the base class of their main activity.
The sample project referenced in the github issue inherits from FormsApplicationActivity (non AppCompat):
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
The docs also mention that modern projects use AppCompat:
Xamarin.Forms Android projects now use FormsAppCompatActivity as the base class of their main activity. This class uses AppCompat features provided by Android to implement Material Design themes.

Single library for Android and iOS

I'm new to Xamarin and have developed Xamarin Class Library for iOS and Android separately. The library has some business logic that launches a ViewController for iOS and Activity for Android. The generated binary for iOS and Android is iOS.dll and Android.dll respectively. These libraries are then consumed in Xamarin.iOS and Xamarin.Android native apps.
There are some points in the business logic that are same for Android and iOS. Is there a way to develop a single library that has the entire business logic with conditional checks for both the platforms for different logics.
I looked into Portable Class Library, but it asks to include Xamarin.Forms for using it. I'm not using Xamarin.Forms in any part of the app. I get the below error when I use the library
PCL Code
if (Device.OS == TargetPlatform.iOS)
{
Lib_iOS.Helper helper = new Lib_iOS.Helper();
helper.launchScreen(parameters, callback);
}
else if (Device.OS == TargetPlatform.Android)
{
Lib_Android.Helper helper = new Lib_Android.Helper();
helper.launchScreen(parameters, callback);
}
Error
You MUST call Xamarin.Forms.Init(); prior to using it.
Note: As a part of business requirement, I need to use only the .dll in the iOS and Android application
Please see the official article about code sharing options: https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/code-sharing.
If you want to use the same code with platform-specific conditions, you can create a shared project. Conditions will look like
#if __ANDROID__
...
#else
...
#endif

How to enable Xamarin app as share target in Xamarin.Forms?

I have a Xamarin.Forms application that I would like to enable as a text and/or URL share target. I've done this in a Windows UWP app by handling OnShareTargetActivated, but I don't see an equivalent in Xamarin.Forms. I am willing to use compiler directives if needed; I would prefer that to doing iOS/Android/Windows specific things in the corresponding projects themselves. Right now all my code is in the Xamarin.Forms project and I'd like to keep it that way.
Unfortunately Android and Ios do not have a similar share event so there is no equivalent event in xamarin forms.

How to use Custom Renderer in Xamarin Shared Project

I am using Shared Project for my cross platform mobile application and want to use Custom renderer in my app for native support. Is there any way i can use Custom renderer in my app.
There is no difference in-between using custom renders in a PCL or Shared Project as seen here. You still have a project per platform. That doesn't change. The real advantage is that you can use ifdefs when compared to PCLs.
here is all the information you'll need in regards to implementing custom renders. And here is an example of a custom renderer for an Entry.
For Xamarin shared base project we have to set local in xmlns below format
xmlns:local="clr-namespace:Sampleforms"
For Xamarin PCL base project we have to set local in xmlns below format
xmlns:local="clr-namespace:Sampleforms;assembly=Sampleforms"
Reming everything will be same as what do for custom render.

Xamarin Integrating Platform Specific Components into Cross Platform Code

I have a simple cross platform app written in Xamarin with VS. All the UI is built in a shared (portable) class so it's written once (using Xamarin Forms) and covers both Android and iOS.
I now just want to add simple loading HUD and I cannot see any good cross platform HUDs. They're all just either written for iOS or Android. I just want to know how I can add a component to the Android and iOS project and access that from the shared class. i.e. Have a method in my shared class called ShowLoading() which loads the iOS component on iOS and android in Android (funny that).
Any ideas?
Thanks
Here is a github project for cross-platform progress hud (it uses ANDHud and BTProgressHud under the covers). https://github.com/aritchie/acr-xamarin-forms
According to the examples, you could do something as simple as:
private readonly IUserDialogService dialogService;
public ICommand Alert {
get {
return new Command(async () => {
await dialogService.AlertAsync("Test alert", "Alert Title", "CHANGE ME!");
this.Result = "Returned from alert!";
});
}
}
You can use a Dependancy Service to access platform specific code from your common code.
I would also recommend having a look at the BT Progress HUD component (for iOS), and the And HUD component (for android) for your loading hud. They can be used together in a cross platform manner, via the XHUD API.

Resources