如何在Xamarin中找到当前的UIViewController [英] How to find current UIViewController in Xamarin

查看:101
本文介绍了如何在Xamarin中找到当前的UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebook Auth SDK , Xamarin Forms C#示例。但是,Facebook SDK已弃用该方法,并用一种​​方法代替了该方法,该方法在构造函数中添加了 fromViewController 变量。我对Xamarin中的ViewControllers概念并不特别满意,或者对示例中的这段代码确实不满意,所以有没有办法评估当前的ViewController?

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 fromViewController variable into the constructors. I am not particularly comfortable with the concept of ViewControllers in Xamarin, or indeed with this code as it is from a sample, so is there a way to gauge the current ViewController?

我已经看到了一些.net示例,例如 NSArray * viewContrlls = [[self navigationController] viewControllers]; [viewContrlls lastObject]; 但是,这种方法似乎不适用于Xamarin,因为 self 不包含navigationController的定义。

I have seen a few .net examples e.g. NSArray *viewContrlls=[[self navigationController] viewControllers];[viewContrlls lastObject]; However, this approach doesn't seem to work with Xamarin, as self doesn't contain definitions for navigationControllers.

或者,是否可以使用示例代码轻松地计算出当前ViewController存放的哪个变量?

Alternatively, is there any way of easily working out which variable my current ViewController is sotored in, using the sample code?

推荐答案

执行此操作的最佳方法是传入对调用Auth方法的ViewController的引用。

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

但是,您也可以尝试这种方法(由 Xamarin论坛

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;
}

这篇关于如何在Xamarin中找到当前的UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆