覆盖后退键在类库中按,WP8可以吗?

时间:2013-10-04 03:37:58

标签: c# user-controls windows-phone-8 popup class-library

我将PhoneApplicationPage实例传递给类库,并在此类库中弹出用户控件,当我按下后退按钮时,整个应用程序退出。昨天我在应用程序中解决了这个问题,但我不能在这个类库中使用该方法。 我试图订阅事件(BackKeyPress),但VS2012说“parent_BackKeyPress”“System.EventHandler”覆盖和委托无法匹配。我检查过,他们匹配。

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

这里有什么不对吗?另外,我可以在classlibrary中使用navigationservice吗?之前我这样做是为了导航到类库中创建的页面,如下所示,它最终会崩溃。有人说不能在classlibrary中使用页面,而是应该使用Popup(usercontrol)。  mContext.NavigationService.Navigate(new Uri(“/ ChangePINPage.xaml”,UriKind.Relative));

1 个答案:

答案 0 :(得分:1)

我已成功完成了这项工作:

// 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;
    };

当然,当且仅当CurrentPage是主页时,您必须确保执行此代码。

我也在类库中使用Pages。您可以在类库中使用NavigationService:您可以从上面获得的当前页面(currentPage.NavigationService)获取它。或者您可以使用PhoneApplicationFrame的Navigate方法:

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

由于像“/SamplePage.xaml”这样的短Uris可以在Application Project中使用,要导航到类库中的页面,你必须提供完整的位置:“/ ClassLibraryName; component / SamplePage.xaml”。

但是请注意,如果应用程序选择显示消息框停止退出,它将不会通过认证,因为(来自Technical certification requirements for Windows Phone):

  

5.2.4.2 - 后退按钮:第一个屏幕

     

在应用的第一个屏幕中按“返回”按钮必须关闭该应用。