Pregunta

I pass a PhoneApplicationPage instance to a classlibrary, and popup an usercontrol in this classlibrary, when I press back button, the whole application exit. Yesterday I sovled the problem in an application, but I cannot use the method in this classlibrary case. I tried to subscribe to the event(BackKeyPress), but VS2012 says "parent_BackKeyPress" "System.EventHandler" override and delegate cannot match. I checked, they match.

PhoneApplicationPage mContext=...; mContext.BackKeyPress += new EventHandler(parent_BackKeyPress); void parent_BackKeyPress(CancelEventArgs e) { ppChangePIN.IsOpen = false; Application.Current.RootVisual.Visibility = Visibility.Visible; }

anything incorrect here? plus, can I use navigationservice in classlibrary? I did this before to navigate to a page created in the classlibrary like below, well it ends up crashing. Some say can't use pages in classlibrary, instead we should use Popup(usercontrol). mContext.NavigationService.Navigate(new Uri("/ChangePINPage.xaml", UriKind.Relative));

¿Fue útil?

Solución

I have successfully done just that:

// or some other method of accessing the current page
// - but via Application, to which you have access also in class library
var currentPage = (PhoneApplicationPage)((PhoneApplicationFrame)Application.Current.RootVisual).Content;
currentPage.BackKeyPress += (sender, args) =>
    {
        // Display dialog or something, and when you decide not to perform back navigation:
        args.Cancel = true;
    };

Of course you have to make sure that this code is executed if and only if the CurrentPage is the main page.

I also use Pages in class library. You can use NavigationService in class library: you can get it for example from current page obtained as above (currentPage.NavigationService). Or you could use the Navigate method of PhoneApplicationFrame:

((PhoneApplicationFrame)Application.Current.RootVisual)
    .Navigate(
        new Uri(
            "/ClassLibraryName;component/SamplePage.xaml", 
            UriKind.Relative));

As the short Uris like "/SamplePage.xaml" will work in Application Project, to navigate to page in class library you have to give full location: "/ClassLibraryName;component/SamplePage.xaml".

But note, that if the application chooses to display message box to stop from exiting, it will not pass certification, as (from Technical certification requirements for Windows Phone):

5.2.4.2 – Back button: first screen

Pressing the Back button from the first screen of an app must close the app.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top