[英]Override BackKeyPress in a classlibrary, is that possible on WP8?
I pass a PhoneApplicationPage instance to a classlibrary, and popup an usercontrol in this classlibrary, when I press back button, the whole application exit. 我将一个PhoneApplicationPage实例传递给一个类库,并在该类库中弹出一个用户控件,当我按返回按钮时,整个应用程序退出。 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. 我试图订阅该事件(BackKeyPress),但是VS2012说“ parent_BackKeyPress”“ System.EventHandler”重写和委托无法匹配。 I checked, they match. 我检查了一下,他们匹配了。
PhoneApplicationPage mContext=...; PhoneApplicationPage mContext = ...; mContext.BackKeyPress += new EventHandler(parent_BackKeyPress); mContext.BackKeyPress + =新的EventHandler(parent_BackKeyPress); void parent_BackKeyPress(CancelEventArgs e) { ppChangePIN.IsOpen = false; void parent_BackKeyPress(CancelEventArgs e){ppChangePIN.IsOpen = false; Application.Current.RootVisual.Visibility = Visibility.Visible; Application.Current.RootVisual.Visibility =可见性.Visible; } }
anything incorrect here? 这里有什么不对吗? plus, can I use navigationservice in classlibrary? 另外,我可以在班级图书馆中使用Navigationservice吗? 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). 有人说不能在类库中使用页面,而应该使用Popup(usercontrol)。 mContext.NavigationService.Navigate(new Uri("/ChangePINPage.xaml", UriKind.Relative)); mContext.NavigationService.Navigate(new Uri(“ / ChangePINPage.xaml”,UriKind.Relative));
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. 当然,您必须确保仅当CurrentPage是主页时才执行此代码。
I also use Pages in class library. 我还在类库中使用Pages。 You can use NavigationService in class library: you can get it for example from current page obtained as above ( currentPage.NavigationService
). 您可以在类库中使用NavigationService:例如,您可以从如上所述获得的当前页面( currentPage.NavigationService
)中获取它。 Or you could use the Navigate method of PhoneApplicationFrame: 或者,您可以使用PhoneApplicationFrame的Navigate方法:
((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". 由于像“ /SamplePage.xaml”这样的简短Uris将在Application Project中工作,因此要导航到类库中的页面,您必须提供完整的位置:“ / 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 ): 但是请注意,如果应用程序选择显示消息框以阻止退出 ,它将不会通过认证,例如(来自Windows Phone的技术认证要求 ):
5.2.4.2 – Back button: first screen 5.2.4.2 –后退按钮:第一个屏幕
Pressing the Back button from the first screen of an app must close the app. 在应用程序的第一个屏幕上按“后退”按钮必须关闭该应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.