What is the proper way to register view with viewmodels in Prism.DryIoc - xamarin.forms

I'm using the Prism Template Pack for Visual Studio for Mac to generate a new project (tried both shared and pcl) then updating to 7.0.0.340-ci. Is registration of the views to view models done by convention?
When I try to run this app it throws this exception: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch.
Here is the code for the main app.
public partial class App : PrismApplication
{
public App(IPlatformInitializer initializer = null) : base(initializer) { }
protected override void OnInitialized()
{
InitializeComponent();
NavigationService.NavigateAsync("MainPage?title=Hello%20from%20Xamarin.Forms");
}
protected override void RegisterTypes(Prism.Ioc.IContainerRegistry containerRegistry)
{
Prism.Mvvm.ViewModelLocationProvider.Register<MainPage,MainPageViewModel>();
}
//protected override void RegisterTypes()
//{
// Container.RegisterTypeForNavigation<MainPage>();
//}
}
I had to comment out the bottom lines and redo the override due to incompatible signatures.
Where did the Container.RegisterTypeForNavigation go or what is it's replacement?
I also tried it without any code in the RegisterTypes method.
In a debug session exploring the NavigationService properties says MainPage is null.

The RegisterTypes method should look like this.
In prism forms 7.x pages used for navigation must be registered via RegisterForNavigation<>() or RegisterForNavigation().
protected override void RegisterTypes(Prism.Ioc.IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<MainPage,MainPageViewModel>();
}

Related

PRISM module with DI unable to navigate view

Quite new to Prism's module and I am using prism 7, I have a module that registers its View/ViewModel and other services:
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<LoginView, LoginViewModel>();
containerRegistry.RegisterSingleton<ILoginManager, LoginManager>();
containerRegistry.RegisterSingleton<ILoginAPIService, LoginAPIService>();
}
The other services that are included inside the manager/apiservice are registered in my App.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<IServiceEntityMapper, ServiceEntityMapper>();
}
Who has a ViewModel constructor
ILoginManager LoginManager;
public LoginViewModel(INavigationService navigationService, IPageDialogService pageDialogService, ILoginManager loginManager) : base(navigationService, pageDialogService)
{
LoginManager = loginManager;
}
However, whenever I inject the ILoginManager seems like I cannot navigate to it, it doesn't give me any exception nor crashes, is there something wrong with how I register my services? Also, removing the ILoginManager able me to navigate to it again
Fixed my problem, it was my APIServiceBase that was causing the error as my empty string base address cannot be converted into Uri.

How to link button event.click with xamarin.driod from xamarin.form

Am new to xamarin.form having a blockage...
This is what I want to do.
I have a button in welcome.xaml in xamarin.form and I want to perform a click event but I want a method from xamarin.driod to be implemented in the click.event.
Those this makes sense to anyone?
You need to write a custom control.
So you could handle the click event in the Android or IOS.
Read here about how to create a custom controls in xamarin.forms
You could also assign a static class that exist in the shared project from your driod project.
I typically prefer using IOC and Interfaces to implement platform specific logic in the Core project, but simple way to do it may just be to set a static Action on your pages code behind.
public partial class WelcomePage
{
public static Action DroidAction { get; set; }
private void Button_OnClicked(object sender, EventArgs e)
{
if (DroidAction == null)
{
Debug.WriteLine("Action was not set");
}
DroidAction?.Invoke();
}
Your Droids OnCreate could be an option of where to do this.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
WelcomePage.DroidAction = new Action(() => Debug.WriteLine("I a platform specific action!"));
}

Is it possible to delay-load PRISM / Xamarin Forms components that aren't immediately needed?

I have the following AppDelegate which takes quite some time to load:
Syncfusion.ListView.XForms.iOS.SfListViewRenderer.Init();
new Syncfusion.SfNumericUpDown.XForms.iOS.SfNumericUpDownRenderer();
Syncfusion.SfCarousel.XForms.iOS.SfCarouselRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfSegmentedControlRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfCheckBoxRenderer.Init();
new Syncfusion.XForms.iOS.ComboBox.SfComboBoxRenderer();
//Syncfusion.XForms.iOS.TabView.SfTabViewRenderer.Init();
new Syncfusion.SfRotator.XForms.iOS.SfRotatorRenderer();
new Syncfusion.SfRating.XForms.iOS.SfRatingRenderer();
new Syncfusion.SfBusyIndicator.XForms.iOS.SfBusyIndicatorRenderer();
What options should I consider when I know some of these components aren't needed for the main screen, but for subscreens?
I am using PRISM, and it appears that every tab is pre-loaded immediately before allowing display or interaction with the end user. What can I do to delay the pre-rendering that the Prism TabView does prior to showing the interface?
Should I use Lazy<T>? What is the right approach?
Should I move these components to another initialization section?
There are a number of ways you could ultimately achieve this, and it all depends on what your real goals are.
If your goal is to ensure that you get to a Xamarin.Forms Page as fast as possible so that you have some sort of activity indicator, that in essence says to the user, "it's ok I haven't frozen, we're just doing some stuff to get ready for you", then you might try creating a "SpashScreen" page where you do additional loading. The setup might look something like the following:
public partial class AppDelegate : FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App(new iOSInitializer()));
return base.FinishedLaunching(app, options);
}
}
}
public class iOSInitializer : IPlatformInitializer, IPlatformFinalizer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance<IPlatformFinalizer>(this);
}
public void Finalize()
{
new Syncfusion.SfNumericUpDown.XForms.iOS.SfNumericUpDownRenderer();
Syncfusion.SfCarousel.XForms.iOS.SfCarouselRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfSegmentedControlRenderer.Init();
Syncfusion.XForms.iOS.Buttons.SfCheckBoxRenderer.Init();
}
}
public class App : PrismApplication
{
protected override async void OnInitialized()
{
await NavigationService.NavigateAsync("SplashScreen");
}
}
public class SplashScreenViewModel : INavigationAware
{
private IPlatformFinalizer _platformFinalizer { get; }
private INavigationService _navigationService { get; }
public SplashScreenViewModel(INavigationService navigationService, IPlatformFinalizer platformFinalizer)
{
_navigationService = navigationService;
_platformFinalizer = platformFinalizer;
}
public async void OnNavigatedTo(INavigationParameters navigationParameters)
{
_platformFinalizer.Finalize();
await _navigationService.NavigateAsync("/MainPage");
}
}
If you're working with Modules you could take a similar approach though any Modules that would initialize at Startup would still be making that call to Init the renderers before you've set a Page to navigate to. That said, working with Modules does give you a number of benefits here as you only ever would have to initialize things that the app actually requires at that point.
All of that said I'd be surprised if you see much in the way of gain as these Init calls are typically empty methods only designed to prevent the Linker from linking them out... if you aren't linking or have a linker file you could simply instruct the Linker to leave your Syncfusion and other libraries alone.

Xamarin.Forms - OnStop(), OnExit(), OnClose()

I'm developing an app and I need to know when the app gets Stopped, Closed, Exited, whatever interrupts it, in order to stop some services such as WebSocket. How can I get 'access' to those events?
Thanks!
I have tested the following in a small example. (Tested it on UWP and works, the OnSleep() is called, when i close the App). The OnSleep() Method which can be overridden in the App.xaml.cs is the Method you are looking for.
The Xamarin Application LifeCycle offers some methods for your needs.
OnStart - Called when the application starts.
OnSleep - Called each time the application goes to the background.
OnResume - Called when the application is resumed, after being sent to the background.
Note that there is no method for application termination. Under normal
circumstances (ie. not a crash) application termination will happen
from the OnSleep state, without any additional notifications to your
code.
Example:
using System;
using System.Diagnostics;
using Xamarin.Forms;
namespace App1
{
public partial class App : Application
{
public App()
{
InitializeComponent();
if (Device.RuntimePlatform == Device.iOS)
MainPage = new MainPage();
else
MainPage = new NavigationPage(new MainPage());
}
protected override void OnStart() {
Debug.WriteLine("OnStart");
}
protected override void OnSleep() {
Debug.WriteLine("OnSleep");
}
protected override void OnResume() {
Debug.WriteLine("OnResume");
}
}
}
Update
According to this you have to catch unhandled exceptions in the native code. That makes it a lil complicated to shutdown your services.
Example:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
protected override void OnCreate(Bundle bundle) {
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) {
//crashed by exception
}
}
Further Reading on Unhandled Exceptions: here

Structure Map Error Code 202

I am fairly used to using Structure Map IoC with MVC, but have recently had to adapt it to legacy web forms.
I have done this by providing a IoCContainer class that has a Configure() method on it such as the following that handles the registry.
public static void Configure()
{
var log = LogManager.GetLogger(typeof (Global1));
log.Debug("Initializing Container");
/*Initialize IoC Container*/
ObjectFactory.Initialize(x =>
{
x.For<ITokenService>().Use<TokenService>();
x.For<ILocalizationService>().Use<LocalizationService>();
log.Debug("Initializing Object Factory");
/*Setup Property Injection*/
x.SetAllProperties(p =>
{
p.OfType<ITokenService>();
p.OfType<ILocalizationService>();
});
});
}
I have provided a base control that contains the BuildUp() method BasePage.cs, such as the following.
public BasePage()
{
var log = LogManager.GetLogger(typeof (Global1));
log.Debug("Base Page Build Up");
/*Inject Dependencies*/
ObjectFactory.BuildUp(this);
}
In one of my classes I am using autowiring to inject the LocalizationService into an ILocalization Property.
Everything works perfectly fine on my local machine, but for some reason, when I deploy this code to our staging server, it is giving me the following StructureMap Exception:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily SuperShuttle.Web.Applications.Reservations.Interfaces.Services.ILocalizationService, SuperShuttle.Web.Applications.Reservations, Version=3.6.27.0, Culture=neutral, PublicKeyToken=null
Not quite sure why it would change when I deploy my application, especially since I am just copying and pasting the files.
Edit I am calling IoCContainer.Configure() in Application_Start() of Global.asax such as the following:
protected void Application_Start(object sender, EventArgs e)
{
log.Debug("In Application Start - Pre BuildUp");
/*Configure Ioc Container*/
IocConfigurator.Configure();
}

Resources