How to find current UIViewController in Xamarin

I am using the Facebook Auth SDK , with the Xamarin Forms C # example . However, the Facebook SDK discounted the method and replaced it with one that adds the fromViewController variable to the constructors. I don't really like the concept of ViewControllers in Xamarin, or is it really with this code, like with the sample, so is there a way to evaluate the current ViewController?

I saw some .net examples, for example. NSArray *viewContrlls=[[self navigationController] viewControllers];[viewContrlls lastObject]; However, this approach does not seem to work with Xamarin, since self does not contain definitions for navigationControllers.

Alternatively, is there any way to easily develop the variable that the current current ViewController changes into using a sample code?

+5
source share
2 answers

The best way to do this is to pass a reference to the ViewController calling the Auth method.

However, you can also try this approach (courtesy of AdamKemp on the Xamarin Forums )

 var window= UIApplication.SharedApplication.KeyWindow; var vc = window.RootViewController; while (vc.PresentedViewController != null) { vc = vc.PresentedViewController; } 
+7
source

The accepted answer will not give you the current view controller if it is on the stack of the parent UINavigationController , so I came up with the following:

 var window = UIApplication.SharedApplication.KeyWindow; var vc = window.RootViewController; while (vc.PresentedViewController != null) vc = vc.PresentedViewController; var navController = vc as UINavigationController; if (navController != null) vc = navController.ViewControllers.Last(); 
+6
source

Source: https://habr.com/ru/post/1239749/


All Articles