PushPopupPageAsync not found in Xamarin Forms Prism - xamarin

Not found PushPopupPageAsync
_navigationService.PushPopupPageAsync( "MyPopupPage" );
I am using Rg.Plugin.Popup along withg Prism
but PushPopupPageAsync not available on INavigationService
how to solve it?

Are you sure that you should be calling PushPopupPageAsync? Rg.Plugin.Popup extends the Navigation service with PushPopupAsync - note there is no Page in the name.
To be able to use PushPopupAsync you also need to have:
using Rg.Plugins.Popup.Extensions;
in the source file where you want to call PushPopupAsync

Its easier to use the Prism.Plugin.Popups made by the Prism maintainer, then you can just use the standard NavigateAsync method:
_navigationService.NavigateAsync("MyPopupPage")
With this, te page is pushed as a popup with Rg.Plugin.Popup. (of course you have to target a PopupPage to make this works)

Related

Xamarin Forms - calling a shared code method from the platform project

I have read the two other questions on SO regarding this and I wanted to know if there is a good solution for that now / best practice.
Long story short, we use an SDK which is written natively and we've wrapped it so that it works on Xamarin.Android and Xamarin.iOS. It has asynchronous callback methods. I need to call a method in the shared code when a callback is received in the Android project for instance.
There's a lot of info for doing the opposite - using DependencyService. How about in my scenario? Does anyone have experience with an app like this and what's the best approach to keep code clean and do this using MVVM?
The options I know are:
Using a static App instance - this is what we currently do.
MessagingCenter
Anything else?
Actually I've never seen anyone recommend usage of MessagingCenter for anything else than communication between ViewModels so I am not sure it is recommended here. Also, I need to know the sender object type so I need a reference to the class in the platform specific project.
I would recommend you to use messagingCenter to pass data or call method between shared project and platform project. You can just send a new object instead of the class in the platform specific project.
Also, have a look at using eventhandler as I mentioned in this answer may help someone who want to call from the shared project into the platform specific one.
BTW, I mean you can even pass an object as TSender if it is not necessary to use:
MessagingCenter.Send<Object>(new object(), "Hi");
MessagingCenter.Subscribe<Object>(new object(), "Hi", (sender) =>
{
// Do something whenever the "Hi" message is received
});

How to call a Javascript function inside an webview on nativescript?

How to call a Javascript function (and register a callback function to be executed after the method execution from web view is done) inside an webview on nativescript?
First the bad new, nothing is "built" in to do this. This requires you to add some code to communicate back from the webview. In the nativescript-webworkers plugins that I wrote; the way I did it was I used the onJsConfirm function on Android. On iOS I used the userContentControllerDidReceiveScriptMessage callback to communicate back. If you want to develop your own code; just grab a copy of my webworkers plugin and you can see the way to do it.
Now to the good news, is that it appears there is already a plugin called "nativescript-webview-interface" which gives you this facility so you don't have to build all that binding code yourself. It looks like all you need to do is include it and you will have bi-directional communications with your webview on both iOS and Android.
Note; to see if a plugin exists in the NativeScript community; your best bet is to look on http://plugins.nativescript.rocks, that is how I found out about the above listed plugin.

MvvmCross 4.x in shared project

I am totally new in developing with Xamarin and I try to use the MvvmCross library in a shared project for all platforms. It seems that I succesfully added the libraries in each project, but now I am confused how to start. I tried to follow the documentation, but I think I didnt understand it right. I can use the CreatableTypes Method, but there are no EndingWith or the other Methods from the documentation. The examples that I found are mostly about PCL. I even dont know if this is a version problem or if I missed something important at all. Any hint or link would be helpfull.
First of all be careful what informations you read, always check the date or you will waste your time with some articles which are outdated. The most important ressources are prolly the official sample: https://github.com/MvvmCross/MvvmCross-Samples
Normally you use the PCL for all the background stuffand there should be some class like this https://github.com/MvvmCross/MvvmCross-Samples/blob/master/TipCalc/TipCalc.Core/App.cs . In this class you should be able to register all your services like this for IoC:
public override void Initialize()
{
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsSingleton();
And then MVVMCross automatically takes care of initializing it, if you do a constructor injection in the ViewModel as a Example...
public MainViewModel(IPreferenceService preferenceService)
{
// Do Something here with the Service
}
...and you just need to call to show it with it connected view:
ShowViewModel<MainViewModel>();
So basically try to understand the samples first, i think TipCalc is still mostly up to date, atleast it is MVVMCross 4.0 https://github.com/MvvmCross/MvvmCross-Samples/tree/master/TipCalc

How to get Current Page/View or topmost Page/View in Xamarin.Forms

I want to show AlertDialog(DisplayAlert) in a Xamarin.Forms project when an unhandled error occurs.
How can I get the current page instance?
You need to keep a reference of your main page as you mentioned. There are a few ways to do this.
You can have a static reference to it in your App.cs file, which is actually already there as it inherits from Application which has:
App.Current.MainPage
If you use an MVVM helper like MVVMLight you pass the page to the service so it keeps a reference of it.
ACR UserDialogs is a dialog package that can also help as it extends and adds different types of dialogs
To get around this and have a lot more flexibility add 'ACR User Dialogs' package to each of your platform's projects.
Then you can use that from anywhere :
await UserDialogs.Instance.AlertAsync ("Your question has been successfully sent", "Thankyou");

Xamarin XLabs IOC and camera

I'm looking at https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs to try and get the camera available to my app.
I have this setup the same, but when I run my app the iOS app that calls my PCL is giving the error:
IResolver has not been set. Please set it by calling Resolver.SetResolver(resolver) method.
I don't know what this means or what exact code I need. I don't use IOC at all and I don't much care about it but I just want this camera to be available to my PCL. How can I get this camera available to my PCL when I'm not using MVC here.
If you wish to use the ViewModel then modify the constructor to take in IMediaPicker:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L62
public CameraViewModel(IMediaPicker mediaPicker)
{
this._mediaPicker = mediaPicker;
}
Remove the Setup function:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/Samples/XLabs.Sample/ViewModel/CameraViewModel.cs#L156-L167
How you get the IMediaPicker implementation to PCL is up to you. IoC is the preferred (and easy) way but you can use other patterns as well. One would be to define a static Func<IMediaPicker> property on the ViewModel and set it in the platform specific projects.
IoC container is the easiest and quickest way to get it done and it would as simple as adding these lines at the application startup (e.g. AppDelegate's FinishedLaunching method):
var container = new SimpleContainer();
container.Register<IMediaPicker, MediaPicker>();
Resolver.SetResolver(container.GetResolver());

Resources