How to find current UIViewController in Xamarin

后端 未结 2 1396
我在风中等你
我在风中等你 2020-12-10 15:54

I am using the Facebook Auth SDK, with a Xamarin Forms C# example. However, the Facebook SDK has depreciated the method and replaced it with one which adds a fromViewC

相关标签:
2条回答
  • 2020-12-10 16:34

    The best way to do this is to pass in a reference to the ViewController that is 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;
    }
    
    0 讨论(0)
  • 2020-12-10 16:45

    The accepted answer won´t give you the current view controller if it´s in the stack of a parent UINavigationController, so I came up with the following:

    public static UIViewController GetTopViewController()
    {
        var window = UIApplication.SharedApplication.KeyWindow;
        var vc = window.RootViewController;
        while (vc.PresentedViewController != null)
            vc = vc.PresentedViewController;
    
        if (vc is UINavigationController navController)
            vc = navController.ViewControllers.Last();
    
        return vc;
    }
    
    0 讨论(0)
提交回复
热议问题