c# - 从 UIApplication 继承类中导航

标签 c# ios xamarin xamarin.ios

我已经考虑了好几个小时了,但无济于事。我正在尝试向我的 iPad 应用程序添加一种方法,只要 120 秒没有任何事件,它就会将其发送回起始屏幕。

我对 Xamarin 和 IOS 编程都很陌生,如果我从完全错误的角度来处理这个问题,我深表歉意。 我创建了一个看起来像这样的类

[Register("ElectronicReceptionMain")]
public class ElectronicReceptionMain : UIApplication
{       
    public override void SendEvent(UIEvent uievent)
    {
        Debug.WriteLine("SendEvent");            
        var allTouches = uievent.AllTouches;
        if(allTouches.Count > 0)
        {
            ResetIdleTimer();
        }            
        base.SendEvent(uievent);
    }

    NSTimer idleTimer;
    void ResetIdleTimer()
    {            
        idleTimer = NSTimer.CreateScheduledTimer(TimeSpan.FromSeconds(120), RefreshScreen);
    }

    void RefreshScreen(NSTimer obj)
    {
        Debug.WriteLine("Elapsed");                      
        UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null);
        ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController;
        SharedApplication.KeyWindow.RootViewController.NavigationController.PushViewController(uvc, true);

    }
}

这个计时器工作正常,(调试消息打印出来没有失败)。但是我无法让 NavigationController 成为 null 以外的任何东西,这显然会导致 null 引用异常。

我也试过

uvc.NavigationController.PushViewController(uvc, true);

同样的问题。我的 Storyboard 中确实有一个导航 Controller ,并且在从一个 UIView 转到另一个 UIView 时在屏幕之间成功导航。

我一次单步执行代码 1 行,肯定是 NaviagtionController null

如有任何帮助,我们将不胜感激。

提前致谢!

最佳答案

您的 RootViewController 应该是 UINavigationViewcontroller。在您的 AppDelgate 中,您需要创建一个 UINavigationViewController 实例并在其中传递您的 InitialViewControllerInstance。并将 UINavigationController 的实例存储在一个变量 (navController) 中,如下所示:

AppDelegate.cs

public UINavigationController navController;

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        //Here you need to instantiate your first ViewController instance from the storyboard and pass as an argument to the UINavigationController
        navController = new UINavigationController(yourInitialViewControllerInstance);
        Window = new UIWindow(UIScreen.MainScreen.Bounds);
        Window.RootViewController = navController;
        Window.MakeKeyAndVisible ();

        return true;
    }

然后如果你想从任何其他类导航到任何其他 View Controller ,你只需从 AppDelgate 访问 navController 对象的实例并按如下所示导航:

void RefreshScreen(NSTimer obj)
{
      Debug.WriteLine("Elapsed");                      
      UIStoryboard StoryBoard = UIStoryboard.FromName("Main", null);
      ViewController uvc = StoryBoard.InstantiateViewController("StartScreenController") as ViewController;

     //Navigate using the navController Instance from the appDelgate
     ((AppDelegate)UIApplication.SharedApplication.Delegate).navController.PushViewController(uvc, true);
}

关于c# - 从 UIApplication 继承类中导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46913976/

相关文章:

c# - 是否有一种通用方法来定义既不接收 Action 也不接收 Func<Task> 的方法?

c# - 处理这两个依赖于另一个的异步方法的最佳方法是什么?

android - Xamarin android 应用程序在恢复时崩溃

c# - 解码 Web 服务返回的 XML(< 和 > 替换为 < 和 &gt)?

C# 字符串到带时区的 DateTime

ios - 根据 URL 更改 UIWebView 滚动位置

ios - 在 Realm 对象中交换元素

ios - 如何触摸动画对象 iOS

android - 在 Android Xamarin 中的 Activity 开始时更改可绘制的切换按钮

android - Xamarin - 找不到与给定名称匹配的资源(在 'theme' 处,值为 '@style/MyTheme')