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

查看:28
本文介绍了如何在 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 不包含导航控制器的定义.

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.

但是,您也可以尝试这种方法(由 AdamKemp 在 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天全站免登陆