2013-10-08 42 views
1

是否有可能通過其他方法創建Windows Phone 8後退按鈕事件(OnBackKeyPress)?我一直試圖從按鈕點擊或頁面初始化器外部調用該事件。但它會給出錯誤?Windows Phone 8後退按鈕事件(OnBackKeyPress)處理?

OnBackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(OnBackKeyPress); 

沒有重載 'OnBackKeyPress' 匹配委託 'System.EventHandler'

回答

8

只是覆蓋背面按鍵事件像以下,

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
{ 
    //Do your work here 
    base.OnBackKeyPress(e); 
} 
+0

我知道這是一個老問題,但這是實現: ///

///按下硬件後退按鈕時調用。 /// /// 將e.Cancel設置爲true以表明請求已由應用程序處理。 protected virtual void OnBackKeyPress(CancelEventArgs e); 所以不要忘記在將它賦予基地之前設置取消值。 –

2

你可以試試這個

public Page() 
    { 
     InitializeComponent(); 
     BackKeyPress +=PageBackKeyPress; 
    } 

    void PageBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
      // code 
    } 
+0

我不能使用給定的事件處理程序OnBackKeyPress? – gayan1991

+0

@ gayan1991然後你可以使用OnBackKeyPress重寫方法[look @ WinMobiler's answer]。 – asitis

2

只需鍵入「覆蓋」(不帶引號),然後按空格,所有重寫的方法將出現,選擇onBackKeyPress方法。

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
    { 
     base.OnBackKeyPress(e); 

    } 

這個方法會出現,現在你可以在裏面寫你的代碼塊了。