Xamarin Integrating Platform Specific Components into Cross Platform Code - visual-studio

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.

Related

Significant drop in performance after migrating from UIWebView to WKWebView

The project I'm working on was using Xamarin Forms (3.4.0.x) Cross Platform, so obviously the iOS app had the UIWebView built in.
We decided to migrate to WKWebView, which required the Xamarin Forms 4.4 update so we proceeded to do that then added [assembly: ExportRenderer(typeof(WebView), typeof(Xamarin.Forms.Platform.iOS.WkWebViewRenderer))] to the AssemblyInfo.cs in our iOS app, like it says to do here. https://github.com/xamarin/Xamarin.Forms/pull/3346
Now I think that's all I need to do completely migrate to WK, but I have noticed that the app has slowed down significantly, taking ~5x as long to load a page, and on top of that, this loading icon stays on the screen even after the page is done loading
My questions are:
Did I migrate correctly, or are there any steps I'm missing/did wrong? Has anyone run into this loading icon problem before? Please ask me to clarify if I'm leaving information out, I'm new to Xamarin Forms so I'm not sure how to ask the best questions, and this is a big project I'm being introduced to. I have been told there is no custom iOS WebView Renderer though, so everything is done using the default WebView.
Thanks!
It is a native iOS issue caused because of the time it takes to detect phone numbers on the web page. So if you just disable that detection, it should work better.
Here's a sample of how you can do it in Xamarin:
var urlRequest = new NSUrlRequest(new NSUrl("https://www.gooogle.com"));
var config = new WebKit.WKWebViewConfiguration();
config.DataDetectorTypes = WebKit.WKDataDetectorTypes.Address;
var frame = new CoreGraphics.CGRect();
var webView = new WebKit.WKWebView(frame, config);
webView.LoadRequest(urlRequest);
If you followed the link you shared, you should be able to put the code inside the SetElement function of the WkWebViewRenderer. So your code would look like this:
var config = new WebKit.WKWebViewConfiguration();
config.DataDetectorTypes = WebKit.WKDataDetectorTypes.Address;
WebView = new WebKit.WKWebView(WebView.Frame, config);
There's an easier solution that just went public from the Xamarin team. All you need to do is update the Forms Nuget, Visual Studio, and Xamarin.iOS, and then:
... go to your iOS project, open the project properties and add this flag in the additional mtouch arguments field: --optimize=experimental-xforms-product-type.

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

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

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 :)

Building a Tray Application using Xamarin?

Using .NET Core and Xamarin I thought about building an application that would run on both Windows and macOS environments.
The .NET Core part would be used for all backend stuff and provide its functionality as a REST API. .NET Core however, does not have any support for building native UI applications (WPF, ...) so that's a no-go for building a tray application.
So I thought about using Xamarin for this. But I can't seem to figure out if this is at all possible. Nor can I find any leads on how to get started.
Some alternatives I've not yet further investigated:
Mono, but that doesn't have any integration with VS2017 so that's out...
QT, but that's aimed at C++, so that's out as well
Java, but then why bother using .NET Core in the first place...
Electron.js
For Windows, Xamarin.Forms will not officially support WPF till 3.0.
re: https://blog.xamarin.com/glimpse-future-xamarin-forms-3-0/
In terms of a Tray app, that is native Windows code and would not be a part of Xamarin.Forms UI code, but the native Windows code that would show/hide your app's UI (assumably Xamarin.Forms based)
For macOS, Xamarin.Forms is supported via Xamarin.Mac, and is currently in a "preview" release.
As far as the equivalent of a tool tray app on macOS, in Xamarin.Mac you can create a item (icon and/or text based) in the system status bar.
Typically this is done in the NSApplicationDelegate.DidFinishLaunching and is easy as:
statusItem = NSStatusBar.SystemStatusBar.CreateStatusItem(NSStatusItemLength.Variable);
statusItem.Button.Image = new NSImage("moviemadness_icon_dark.png")
{
Template = true
};
statusItem.Button.Action = new Selector("StatusItemButtonAction:");
In this example, any time the icon/text in the status is tapped, the StatusItemButtonAction method is triggered and it either terminates itself if you were holding the control key down at the same time, otherwise it runs the StatusBarPopOver which in my case constructs a NSPopover and shows an embedded Xamarin.Forms TabbedPage UI that shows the mobile app server status, current users of the app, the app analytics, etc...
[Action("StatusItemButtonAction:")]
public void StatusItemButtonAction(NSStatusBarButton sender)
{
var currentEvent = NSApplication.SharedApplication.CurrentEvent;
if (currentEvent.ModifierFlags.HasFlag(NSEventModifierMask.AlternateKeyMask) ||
currentEvent.ModifierFlags.HasFlag(NSEventModifierMask.ControlKeyMask))
NSApplication.SharedApplication.Terminate(this);
else
StatusBarPopOver();
}

Does Xamarin support to develop apps for Windows surface and how well?

I want to develop an app for Windows surface tablet and iOS mobile and iPad.
As I am a new user to Xamarin, when I created my first project,it shows 3 projects:-
hello.driod, hello.ios and hello.winPhone.
I have 3 questions based on this:-
How will I be able to write the same code and share for windows 8.1 and iOS?
and whenever I drag and drop the elements to the UI page, will the same elements be copied to both windows and iOS simultaneously or I got to add them seperately?
Currently I dont have a MAC to connect to my PC. Can I write the code and and there while testing it, connect it to a MAC or should it be connected during the whole process?
Please Help!
Using the same code depends on how your structure your app.
You can go the native route where you can share the bulk of your logic by containing it in a shared / PCL project (more on that here), but have platform specific code for your UI.
For example, if you have a cross platform app targeting iOS and Android you would still create the UI in a storyboard for iOS and AXML files for Android. Any code you want to "connect" to your UI would be specific to that platform as you would use the platform APIs. Any code that is not platform specific (i.e., not calling iOS or Android APIs) can go in your shared / PCL project.
Or you can choose Xamarin.Forms which adds a layer of abstraction by allowing you to write the UI in XAML once and have it work on all platforms. The advantage is increased code sharing as now your UI is also shared. The downside is to utilise platform specific features you'll need to implement DependencyService or custom renderers. Read more about Xamarin.Forms here.
As above, it depends. If you are going the native route, then no. If you are going with Xamarin.Forms, then you are using the same XAML code for the UI across platforms, but there is no drag and drop designer.
To build an iOS app you need to be connected to a Mac. You will also need to be connected to a Mac to use the iOS Designer.

Resources