我正在尝试做一个可以说是个坏主意的事情,但我认为这仍然是可能的。我试图覆盖WP8如何处理后退按钮并自己实现。我认为如果我:

计划

  • 只能在整个应用程序中创建一个“框架”和“页面”。
  • 除非自己要退出应用程序,否则请务必自己处理PhoneApplicationPage.BackKeyPress

  • 再现

    Here's a sample project that has the crash

    编码

    ..then它应该工作。但是,我的尝试被Windows Phone阻止了。 Here's the code:
    // This basically happens on PhoneApplicationService.OnLaunched
    _viewModelChanged.StartWith(ViewModel).Where(x => x != null).Subscribe(vm => {
        var page = default(IViewFor);
        var frame = RootVisual as PhoneApplicationFrame;
    
        // Find the initial PhoneApplicationPage for the app
        page = RxApp.GetService<IViewFor>("InitialPage");
    
        // Depending on how we're being signalled (i.e. if this is cold start
        // vs. resume), we need to create the PhoneApplicationFrame ourselves
        if (frame == null) {
            frame = new PhoneApplicationFrame() {
                Content = page,
            };
        }
    
        page.ViewModel = vm;
        var pg = page as PhoneApplicationPage;
        if (pg != null) {
            pg.BackKeyPress += (o, e) => {
                if (ViewModel.Router.NavigationStack.Count <= 1 ||
                    ViewModel.Router.NavigateBack.CanExecute(null)) {
                    return;
                }
    
                e.Cancel = true;
                ViewModel.Router.NavigateBack.Execute(null);
            };
        }
    
        // Finally, set Application.RootVisual
        RootVisual = frame;
    });
    

    悲伤感

    这很好用,直到执行此代码后,框架排队的DispatcherItem导致应用程序崩溃:
    System.NullReferenceException occurred
    Message: A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Phone.ni.dll
    Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)   Unknown
    Microsoft.Phone.ni.dll!Microsoft.Phone.Controls.PhoneApplicationPage.Microsoft.Phone.Controls.IPhoneApplicationPage.InternalOnNavigatedFromX(System.Windows.Navigation.NavigationEventArgs e)   Unknown
    Microsoft.Phone.ni.dll!System.Windows.Navigation.NavigationService.RaiseNavigated(object content, System.Uri uri, System.Windows.Navigation.NavigationMode mode, bool isNavigationInitiator, Microsoft.Phone.Controls.IPhoneApplicationPage existingContentPage, Microsoft.Phone.Controls.IPhoneApplicationPage newContentPage) Unknown
    Microsoft.Phone.ni.dll!System.Windows.Navigation.NavigationService.CompleteNavigation(System.Windows.DependencyObject content, System.Windows.Navigation.NavigationMode mode)   Unknown
    Microsoft.Phone.ni.dll!System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(System.IAsyncResult result) Unknown
    Microsoft.Phone.ni.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(System.AsyncCallback userCallback, System.Windows.Navigation.PageResourceContentLoader.PageResourceContentLoaderAsyncResult result) Unknown
    Microsoft.Phone.ni.dll!System.Windows.Navigation.PageResourceContentLoader.BeginLoad.AnonymousMethod__0(object args)    Unknown
    [Native to Managed Transition]
    mscorlib.ni.dll!System.Delegate.DynamicInvokeImpl(object[] args)    Unknown
    System.Windows.ni.dll!System.Windows.Threading.DispatcherOperation.Invoke() Unknown
    System.Windows.ni.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority)    Unknown
    System.Windows.ni.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context)  Unknown
    System.Windows.ni.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args)   Unknown
    System.Windows.RuntimeHost.ni.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam* pParams, System.Windows.Hosting.NativeMethods.ScriptParam* pResult)   Unknown
    

    最佳答案

    所以,我已经解决了这个问题-我的代码有问题,因为我不了解WP8的工作原理:)这就是我现在所了解的,这可能也是错误的,但是我还是会写它

    WP8应用程序的初始化方式:

  • 操作系统通过补水App.xaml.cs创建您的App类
  • 这意味着您的构造函数将运行,在此过程中,您将创建一个PhoneApplicationFrame
  • 创建PhoneApplicationFrame似乎还设置了一个全局静态变量(在App.xaml中创建PhoneApplicationService会发生同样的事情,它会设置PhoneApplicationService.Current)。
  • NavigationService然后尝试通过资源字符串(即'/MainPage.xaml')重新创建XAML View 。要么重新创建以前被逻辑删除的对象,要么重新创建,否则它默认为WMAppManifest 中的一个(这是我不了解的部分)。
  • Navigator.Service会调用
  • PhoneApplicationFrame.Navigated-在这里,您实际上可以开始做一些事情,其中​​最重要的是设置Application.RootVisual ,它将把Loading ...屏幕发送给
  • 基本上完成所有设置后,
  • PhoneApplicationService.LaunchedPhoneApplicationService.Activated最终会触发,具体取决于如何唤醒您的应用程序。
  • 关于windows-phone-8 - 覆盖WP8导航-PhoneApplicationPage中的崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14791060/

    10-10 23:28