Using Activity Result API in Xamarin.Android - xamarin

It seems like it's not properly supported as I need to implement JNI stuff when I try to implement IActivityResultCallback interface. Also ActivityResultContract is not a generic class so when I inherit it to create a custom contract it's not type-safe like in native Android. So it's not usable at all at the moment I guess?

You can get around this issue by inheriting from Java.Lang.Object. Something like this will probably work:
public class MyActivityResultCallback : Java.Lang.Object, IActivityResultCallback
{
}
IActivityResultCallback inherits from IJavaObject, which most, if not all Android interfaces do. This way Xamarin can interface between C# and Java. This is described pretty well here: https://learn.microsoft.com/en-us/xamarin/android/platform/java-integration/working-with-jni#implementing-interfaces

Related

How to register HealthCheck in Autofac module

Im using .NET 6 and Autofac to register my dependencies, all works great.
However I was wondering how can I register a healthcheck in my module (not in the startup.cs), ex:
public class InfrastructureModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<ApplicationDbContext>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
// builder.AddCheck<MyServiceHealthCheck>("Service ACheck"); <- not working
}
}
Short answer is: you can't. You can't mix and match Microsoft registrations with Autofac registrations like that. That's why there's ConfigureServices separate from ConfigureContainer.
Longer answer is: if you want to go spelunking through the code of AddHealthCheck and figure out what it's doing under the covers, you could make your own Autofac version of that and it would work. But it's work for you to do, not something Autofac provides, and it'd be supported only by you.
However, reading the intent of the question, it sounds like you want to do MS registrations in an Autofac module and that's not a supported thing.

Shiny + Prism + Platform Service Example

I am attempting to update a Prism 8.1 app to use Shiny v2.
In trying to turn one of my services into a Job I keep getting a container resolution error (using Unity). I am not sure what the pattern is for registering platform implementations of services. The Job has a service that is from my platform project but at the time the services.RegisterJob() is called I guess the platform initializer has not run.
Can someone post an example of how you are supposed to register platform implementations with Shiny?
Well, I'm not sure if this is the intended design but I solved the platform services this way.
I added a constructor parameter to my ShinyStartup like this:
public Startup(IPlatformInitializer platformInitializer) : base(PrismContainerExtension.Current)
{
_platformInitializer = platformInitializer;
}
and then in my AppDelegate I used this:
Shiny.ShinyHost.Init(new Shiny.ApplePlatform(), new Startup(new iOSInitializer()));
Where iOSInitializer is my Prism IPlatformInitializer.
Then in Startup I added:
protected override void RegisterServices(IContainerRegistry containerRegistry)
{
_platformInitializer.RegisterTypes(containerRegistry);
...
}
As far as the IJob not resolving dependencies when using RegisterJob, I moved job registration to App.OnStart using IJobManager.Register and it works. Also not sure if this is the intended design.
I did all my container wire up before calling RegisterJob and it still failed to resolve so there must be something under the covers that is happening in the Prism+Shiny world.

Example of new abstract new abstract CreateContainerExtension method in 7.1.0.172-pre

Does anyone have an example of the new abstract method CreateContainerExtension that is in Prism.Wpf 7.1.0.172-pre? We are using the common service locator and have essentially bypassed IOC in Prism because we need to resolve things before the Bootstrapper has run.
You should remember that Prism is open source and the source code is in itself a form of documentation.
If you're using the classic Bootstrapper, you'll notice that it has been deprecated in favor of PrismApplication. Since your question is extremely vague as to what container you're even trying to use, it's impossible to tell exactly which Container Extension to use, but I will provide an example using Unity for your reference.
Whether you look at the UnityBootstraper or the Unity PrismApplication, you'll see that it simply returns an instance of the UnityContainerExtension.
protected override IContainerExtension CreateContainerExtension()
{
return new UnityContainerExtension();
}

Using a custom renderer in PCL library

I want to use my custom renderer inside a PCL. Is it possible? Or can I initialize my custom renderer inside this PCL?
No and No.
What you use in PCL is - let's say - component and it's abstraction. The 'materialization' (or not) of the component will be made by custom renders on each platform.
I can't see a reason to use it on a platform-independent implementation once it can be shown (or to behaves) differently on each one.
Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/
Maybe with a real case, we can suggest another solution.
Finally, I've found a solution.
I've just created a class in my PCL and used it in XAML, let's say:
public class MyHelperEntry : Entry { public MyHelperEntry() { } }
that inherits Entry class. And in an App, where I use this PCL, I've created a class, that inherits MyHelperEntry:
public CustomHelperEntry : MyHelperEntry { public CustomHelperEntry() { } }
and used this CustomHelperEntry as a custom renderer.

Xamarin, How to invoke Shared code Method from Platform specific Dependency class

In Xamarin forms app, How can we invoke Shared code Method from Platform specific Dependency class.
I need to call one method implemented in my ContentPage class from my iOS dependency class.
Thanks...
There are different solutions to this:
Use a static method to call it where ever you need to.
Use the messaging center to send a message to your Shared/PCL project and do what ever you need. (link: https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/messaging-center/)
If this is a Custom renderer you can use binded command properties and Execute those in your platforms specific code.
In my case, what I did was having a static class called Helper, that contains all the static methods that I need to call on all platforms/projects.
Hope this helps.

Resources