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
Related
I'm trying to follow this tutorial on creating a login page using Auth0: https://auth0.com/docs/quickstart/native/xamarin/01-login#handing-the-callback-url and noticed that the tutorial creates two separate login pages for android and iOS, as if they are two separate apps/projects/solutions. But my app is supposed to be compatible with both android and iOS, so I created a forms page under RoseySports.ios called Login_iOS (As indicated in screenshot 1) and would like to test it out to see if the login page works, but cannot seem to find a way to set the MainPage as Login_iOS (screenshot 2). I would like for it to be so that if a device is running iOS, it will redirect the user to the iOS version of the login page, and vice versa for android. Sorry if I'm not using the correct terminology when describing my issue. The reason I had to create two separate login pages for iOS and android was because i had to use using Auth0.OidcClient; as there are separate Nuget packages for the iOS solution and Android solution, which doesn't work when trying to put it on the main project (the one compatible for both iOS and Android).
And please let me know if there's a way to make just one login page for both platforms using Auth0.
UPDATE:
This is what I've done now, but I'm getting an error at MainPage = new RoseySports.Login_iOS(); saying that Login.iOS does not exist in the namespace RoseySports. This is the rest of the code:
`switch(Device.RuntimePlatform)
{
case Device.iOS:
MainPage = new RoseySports.Login_iOS();
break;
case Device.Android:
MainPage = new Login_Page();
break;
}`
You have multiple questions and in StackOverflow you should not ask them in one question, but let's try to address them:
Pages are only graphic elements and there is no need to implement separate graphic elements in iOS and Android in most cases, and yours doesn't look like a valid one for that
In general you can implement some code as platform specific. If you are using shared projects you can use conditional compiling, if not then dependency injection
You can use some of the technics above to do what you wanted to do (have different versions of pages per platform)
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 :)
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();
}
I have four libraries all in all, a PCL and three platform-specific class libraries for Android, iOS and Win Phone. I have a base class for a custom Xamarin.Forms view in the PCL and renderers for it in the platform-specific libraries.
In the project where I use the libraries, the OnElementChanged() function of the iOS renderer is never called. I have tested on Android thus far, and there it works and the view gets rendered. Why might this be?
To complicate things, I have used the libraries in another project, where they work seamlessly on all three platforms.
I can provide more information if needed.
As there is not much info...
Make sure ExportRendererAttribute is set for the renderer
Make sure only one renderer available for the Control
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.