簡體   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