Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding WP8 Navigation - crash in PhoneApplicationPage
    primarykey
    data
    text
    <p>I'm trying to do something that's arguably a bad idea, but I think it's still possible. I'm trying to override how WP8 handles the Back Button and implement it myself. I theorize that if I:</p> <h3>The Plan</h3> <ol> <li>Only ever create one "Frame" and "Page" in the entire application</li> <li>Always handle <code>PhoneApplicationPage.BackKeyPress</code> myself unless they were about to back out of the application.</li> </ol> <h3>The Repro</h3> <p><a href="http://cl.ly/3R3F3L1X1Y0q" rel="nofollow">Here's a sample project that has the crash</a></p> <h3>The code</h3> <p>..then it <em>should</em> work. However, my attempts are being thwarted by Windows Phone. <a href="https://github.com/reactiveui/ReactiveUI/blob/master/ReactiveUI.Mobile/WP8AutoSuspendApplication.cs" rel="nofollow">Here's the code</a>:</p> <pre><code>// This basically happens on PhoneApplicationService.OnLaunched _viewModelChanged.StartWith(ViewModel).Where(x =&gt; x != null).Subscribe(vm =&gt; { var page = default(IViewFor); var frame = RootVisual as PhoneApplicationFrame; // Find the initial PhoneApplicationPage for the app page = RxApp.GetService&lt;IViewFor&gt;("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) =&gt; { if (ViewModel.Router.NavigationStack.Count &lt;= 1 || ViewModel.Router.NavigateBack.CanExecute(null)) { return; } e.Cancel = true; ViewModel.Router.NavigateBack.Execute(null); }; } // Finally, set Application.RootVisual RootVisual = frame; }); </code></pre> <h3>Sadness</h3> <p>This works great, until right <em>after</em> this code executes, where a DispatcherItem queued by the framework crashes the app:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload