繁体   English   中英

在类库中覆盖BackKeyPress,是否可以在WP8上使用?

[英]Override BackKeyPress in a classlibrary, is that possible on WP8?

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

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

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

我已经成功地做到了:

// 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”。

但是请注意,如果应用程序选择显示消息框以阻止退出 ,它将不会通过认证,例如(来自Windows Phone的技术认证要求 ):

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

在应用程序的第一个屏幕上按“后退”按钮必须关闭该应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM