How to register service at UWP project - xamarin

I am trying to register alert service ( message dialog) at setup.cs for this method
protected override void InitializePlatformServices() {
base.InitializePlatformServices();
Mvx.RegisterSingleton<IAlertService>(new AlertService());
}
but i also have this exception http://prntscr.com/krpbcw

For services which has implementation in native projects.
Go to Setup.cs
protected override IMvxApplication CreateApp(){
Mvx.RegisterType< IAlertService, AlertService >();
}
Know one thing, AlertService implements IAlertService

Related

Replace default IoC container in MvvmCross

Since MvvmCross v7 sticks on its own IoC container, I would like to replace it with the .NET Core one in order to have an easier life when registering third party libraries such as IHttpClientFactory, Polly, Automapper, etc. through already built-in extensions methods.
To achieve this, I've successfully created a class that implementas MvxSingleton<IMvxIoCProvider> described as follow:
public class HostingAdapter : MvxSingleton<IMvxIoCProvider>, IMvxIoCProvider
{
private IServiceProvider ServiceProvider;
private IServiceCollection ServiceCollection;
public HostingAdapter()
{
var host = Host
.ConfigureServices((context, serviceCollection) =>
{
// Configure local services
ConfigureServices(context, serviceCollection);
ServiceCollection = serviceCollection;
ServiceProvider = ServiceCollection.BuildServiceProvider();
})
.Build();
}
public void RegisterType<TFrom, TTo>() where TFrom : class where TTo : class, TFrom
{
ServiceCollection.AddTransient<TFrom, TTo>();
ServiceProvider = ServiceCollection.BuildServiceProvider();
}
public T GetSingleton<T>() where T : class
{
return ServiceProvider.GetRequiredService<T>();
}
public object GetSingleton(Type type)
{
return ServiceProvider.GetRequiredService(type);
}
.. and all the required methods requested by the interface.
Then on the platform specific side I override the IoC creation as follow:
protected override IMvxIoCProvider CreateIocProvider()
{
var hostingAdapter = new HostingAdapter();
return hostingAdapter;
}
The code seems to work but as soon as the app starts Mvx registers its own "extra" services such as the IMvxLoggerProvider, IMvxSettings and so on. And here issues come:
ServiceProvider = ServiceCollection.BuildServiceProvider(); is called during the Host initialization but Mvx still continue to register services after that. This means IServiceProvider is not 'in sync' with IServiceCollection and a new ServiceCollection.BuildServiceProvider(); call is needed. I temporarily solved updating the provider at each collection registration (like the code above) but I'm aware this affects performances. Anyone knows how to workaround this?
There are plenty of Mvx services that are not registered so the app fails to start. These are the IMvxLogProvider, IMvxAndroidLifetimeMonitor, IIMvxSettings, IMvxStart, etc. I just wonder, why? How can let Mvx handle the registration in my container of all what it needs to start? I partially solved some of them such as the logger thing replacing the default with a custom one, but other callbacks like InitializeLifetimeMonitor are called too late for being registered.
Do I need to change anything in my MvxApplication than the most standard implementation?
Am I really forced to replace the standard IoC container? How can I handle the IServiceCollection's extension methods that 3rd party libraries expose like services.AddHttpClient();?
If it needs, I am on Xamarin classic using the Droid platform. Thanks
Deliberately inspired by Unity.Microsoft.DependencyInjection repository I've workarounded this approaching the problem the other way round: instead of replacing the default IoC container, I manually initialize an IServiceCollection instance and I add it to the Mvx's IoC provider.
To achieve this, I've used the following code:
public class App : MvxApplication
{
public override void Initialize()
{
base.Initialize();
InitializeServiceCollection();
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
RegisterAppStart<HomeViewModel>();
}
private static void InitializeServiceCollection()
{
IServiceCollection serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
MapServiceCollectionToMvx(serviceProvider, serviceCollection);
}
private static void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddHttpClient();
}
private static void MapServiceCollectionToMvx(IServiceProvider serviceProvider,
IServiceCollection serviceCollection)
{
foreach (var serviceDescriptor in serviceCollection)
{
if (serviceDescriptor.ImplementationType != null)
{
Mvx.IoCProvider.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType);
}
else if (serviceDescriptor.ImplementationFactory != null)
{
var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
Mvx.IoCProvider.RegisterSingleton(serviceDescriptor.ServiceType, instance);
}
else if (serviceDescriptor.ImplementationInstance != null)
{
Mvx.IoCProvider.RegisterSingleton(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationInstance);
}
else
{
throw new InvalidOperationException("Unsupported registration type");
}
}
}
}

Resolution failed exception in Xamarin IOS prism

I have an iOS application written in Xamarin and I am getting a Unity Exceptions Resolution Failed exception when I try and run the application in iOS. However this error does not happen when I run the android version of the application. The exception is being thrown while the initalize function from prism is taking place.
Here is a snippet from my app.xaml.cs
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
this.RegisterLocal(containerRegistry);
this.RegisterServices(containerRegistry);
this.RegisterPagesForNavigation(containerRegistry);
}
This code all executes and passes.
This is the iOS initialization
Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
PullToRefreshLayoutRenderer.Init();
LoadApplication(new App(new IosInitializer()));
return base.FinishedLaunching(app, options);
}
public class IosInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.Register<IUAirshipUpdate, UAirshipUpdate>();
}
}
This code also executes
The exception being thrown is an argument null exception indicating that IModuleCatelog is not present. I do not understand why it is looking for that module and can not find it. The source code on GitHub indicates that class was registered.
This issue was caused because linker behavior for IOS application was set to full and that causes issues with Unity IOC Container.

Xamarin.Android Architecture Components: Not getting callbacks for lifecycle events

I'm trying to use the architecture components package for detecting when the application enters background or foreground state. The problem is that the callbacks are not being invoked. In the sample code below, the methods onApplicationForegrounded and onApplicationBackgrounded are not invoked:
namespace POC.Droid
{
[Application]
public class MyApp : Application, ILifecycleObserver
{
static readonly string TAG = "MyApp";
public MyApp(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
}
[Lifecycle.Event.OnStop]
public void onAppBackgrounded()
{
Log.Debug(TAG, "App entered background state.");
}
[Lifecycle.Event.OnStart]
public void onAppForegrounded()
{
Log.Debug(TAG, "App entered foreground state.");
}
}
}
My Xamarin version is 8.2.0.16 (Visual Studio Community) and Xamarin.Android.Arch.Lifecycle.Extensions version is 1.0.0. I'm using a Nougat device (7.0) for testing.
TL;DR Please annotate your lifecycle callbacks with [Export]
Here a more detailed description:
Generally, to get the methods of a lifecycle observer be invoked, please make sure that the related packages are present. Here is a part of my packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Xamarin.Android.Arch.Core.Common" version="26.1.0" targetFramework="monoandroid81" />
<package id="Xamarin.Android.Arch.Core.Runtime" version="1.0.0.1" targetFramework="monoandroid81" />
<package id="Xamarin.Android.Arch.Lifecycle.Common" version="26.1.0" targetFramework="monoandroid81" />
<package id="Xamarin.Android.Arch.Lifecycle.Extensions" version="1.0.0.1" targetFramework="monoandroid81" />
<package id="Xamarin.Android.Arch.Lifecycle.Runtime" version="1.0.3.1" targetFramework="monoandroid81" />
This is how this looks in Visual Studio:
To be able to set a lifecycle observer, we need a lifecycle owner. On the application level this can be ProcessLifecycleOwner, just like the original poster showed.
Here is a slightly modified version:
using System;
using Android.App;
using Android.Arch.Lifecycle;
using Android.Util;
using Java.Interop;
namespace Stopwatch_AAC
{
[Application]
public class MyApp : Application, ILifecycleObserver
{
const string TAG = "MyApp";
public MyApp(IntPtr handle, Android.Runtime.JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
public override void OnCreate()
{
base.OnCreate();
ProcessLifecycleOwner.Get().Lifecycle.AddObserver(this);
}
[Lifecycle.Event.OnStop]
[Export]
public void Stopped()
{
Log.Debug(TAG, "App entered background state.");
}
[Lifecycle.Event.OnStart]
[Export]
public void Started()
{
Log.Debug(TAG, "App entered foreground state.");
}
}
}
As you can see, you annotate your lifecycle methods with for example [Lifecycle.Event.OnStop]. Also, please note that you need to use [Export]. Please make sure that Mono.Android.Export is referenced in your project as shown in the following screenshot.
If you want to have lifecycle observers for an activity, I suggest to extend AppCompatActivity as it is a lifecycle owner:
using Android.App;
using Android.Arch.Lifecycle;
using Android.OS;
using Android.Support.V7.App;
using Android.Util;
using Java.Interop;
namespace Stopwatch_AAC
{
[Activity(Label = "Minimal", Exported = true, MainLauncher = true)]
public class Minimal : AppCompatActivity, ILifecycleObserver
{
const string TAG = "Stopwatch_AAC";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Lifecycle.AddObserver(this);
Log.Debug(TAG, Lifecycle.CurrentState.ToString());
}
[Lifecycle.Event.OnAny]
[Export]
public void Hello()
{
Log.Debug(TAG, Lifecycle.CurrentState.ToString());
}
}
}
if you need it in the activities here the events:
protected override void OnStart(){
base.OnStart();
Log.Debug(logTag, "MainActivity.OnStart() called, the activitiy is active");
}
protected override void OnPause()
{
base.OnPause();
Log.Debug(logTag, "MainActivity.OnPause() called, the activity in background");
}
protected override void OnStop()
{
base.OnStop();
Log.Debug(logTag, "MainActivity.OnStop() called, the activity is in background because of other activiy or app");
}
protected override void OnResume()
{
base.OnResume();
Log.Debug(logTag, "MainActivity.OnResume() called, the activity stated");
}
protected override void OnRestart()
{
base.OnRestart();
Log.Debug(logTag, "MainActivity.OnRestart() called, the activity is startet");
}
protected override void OnDestroy()
{
base.OnDestroy();
Log.Debug(logTag, "MainActivity.OnDestroy() called, activity is destroyed");
}
for Xamarin Forms you will find in app.xaml.cs the event which are needed for the apps.
protected override void OnStart ( ) {
// Handle when your app starts
}
protected override void OnSleep ( ) {
// Handle when your app sleeps
}
protected override void OnResume ( ) {
// Handle when your app resumes
}
I have used that package in the past, however I much prefer the implementation by James Montemagno which can be found as a nuget package called "Plugin.CurrentActivity". It creates an application class and implements the ILifecycle events for you.
From the description:
Provides a simple solution for getting access to the current Activity of the application when developing a Plugin for Xamarin.
This will lay down a base "application" class for developers in their Android application with boilerplate code to get them started.
Can be used with Android API 14+
* I am making the assumption that you're not using Xamarin.Forms. This works perfectly for a native Xamarin Android project.
Link to the Github page

org.osgi.service.event.EventHandler fails to listen the events posted by the EventAdmin service

I am trying to run a demo application for OSGi EventAdmin service, but the EventHandler I implemented fails to listen the events posted by the EventAdmin publisher:
Below is the code for Event Publisher, followed by the code for Listener(EventHandler):
public class Publisher implements BundleActivator{
static EventAdmin eventAdmin;
ServiceReference ref;
static HashMap properties= null;
#Override
public void start(BundleContext context) throws Exception {
ref=context.getServiceReference(EventAdmin.class.getName());
if(ref==null){
System.err.println("Unable to aquire EventAdmin Ser Ref.");
}
eventAdmin=(EventAdmin) context.getService(ref);
if(eventAdmin==null){
System.err.println("unable to get service:EventAdmin");
}
properties=new HashMap();
properties.put("XYZ", "Test");
Event event = new Event("lnu/test/event/Demo", properties);
eventAdmin.postEvent(event);
System.out.println("event posted");
}
#Override
public void stop(BundleContext context) throws Exception {
// TODO Auto-generated method stub
}
}
Code for Listener:
public class Listener implements BundleActivator, EventHandler {
public void start(BundleContext context) {
Dictionary d = new Hashtable();
d.put(EventConstants.EVENT_TOPIC, "lnu/test/event/Demo" );
context.registerService( EventHandler.class.getName(),
this, d );
System.out.println("event handler is registered now");
}
public void stop( BundleContext context) {}
public void handleEvent(Event event ) {
System.err.println("Event has been captured");
System.out.println("getTopic: "+event.getTopic());
System.out.println("getproperty: "+event.getProperty("XYZ"));
}
}
The print statements in the code show that the event has been posted by the publisher and the Listener is registered with the EventHandler service but still it does not invokes handleEvent method on the listener side, I don't know why? and can't understand what is happening behind the scene. There are no runtime exceptions/errors.
The IDE used is Eclipse Juno Build id: 20120614-1722 with Equinox.
Following Target Platform bundles are included in the run configuration:
org.eclipse.osgi
org.eclipse.equinox.event
org.eclipse.equinox.util
org.eclipse.osgi.services
Can some one point me what I am missing or doing wrong? Or if you have some link to working example of OSGi EventAdmin service?
I would guess that your listener bundle is being registered after the publisher bundle has already posted the Event.
Testing this in the start methods of the bundles is error prone for this reason unless you control the start order of the bundles. I would suggest for this simple test that you start a separate thread in your publisher to post an event every few seconds. The listener should start getting them once it is registered.
Confirm that your listener bundle is importing the same org.osgi.service.event package as the EventAdmin bundle. It is possible that your listener bundle includes the org.osgi.service.event package and is thus not using the same org.osgi.service.event package as the EventAdmin bundle. This could be why the EventAdmin bundle does not call your EventHandler service. It may be something else, but this is something to check.

How to sink COM events from Silverlight?

I want to raise an event from a .NET class, and receive this event in Silverlight code (out-of-browser, using the COM interop support added in SL4).
Can anyone point out the problem in my code? Do I maybe need to do more attribute-decorated interface boilerplate to get this working?
Code:
Rather than write native COM code, I am writing .NET code and exposing it via COM interop.
My event-raising .NET class looks like this:
using System;
namespace TestComInterop2 {
public class TestClass {
public event EventHandler TestEvent;
public void Fire() {
if (TestEvent != null)
TestEvent(this, EventArgs.Empty);
}
}
}
My SL4 client code looks like this:
...
private delegate void HandlerDelegate(dynamic sender, dynamic eventArgs);
private void TestEventSinking(object sender, RoutedEventArgs e)
{
// Create instance of COM-registered .NET class
var testClass = AutomationFactory.CreateObject("TestComInterop2.TestClass");
// Subscribe to event (second line fails with System.Exception)
//
// "Failed to add event handler. Possible reasons include: the object does not
// support this or any events, or something failed while adding the event."
//
AutomationEvent testEvent = AutomationFactory.GetEvent(testClass, "TestEvent");
testEvent.AddEventHandler(new HandlerDelegate(HandleTestEvent));
// Fire the event
testClass.Fire();
}
private void HandleTestEvent(object sender, object eventargs)
{
MessageBox.Show("Event fired");
}
...
Use the [ComSourceInterface] attribute on your class. The relevant MSDN Library topic is here.

Resources