在RootFrame上阻止BackKeyPress无法正常运行Windows Phone

时间:2014-07-21 08:30:54

标签: windows-phone-7 windows-phone-8 windows-phone

最近我遇到了另一个难以理解的问题。在msdn(http://msdn.microsoft.com/library/windows/apps/hh202868%28v=vs.105%29.aspx)上我们读到:

Also, the BackKeyPress event is available in the PhoneApplicationFrame class. The frame can handle the event directly before the active page receives it and the key press can be canceled

根据这个,我写了一个简单的例子:

private void InitializePhoneApplication()
    {
        if (phoneApplicationInitialized)
            return;

        // Create the frame but don't set it as RootVisual yet; this allows the splash
        // screen to remain active until the application is ready to render.
        RootFrame = new PhoneApplicationFrame();
        RootFrame.Navigated += CompleteInitializePhoneApplication;
        RootFrame.BackKeyPress += App_BackKeyPress;
        // Handle navigation failures
        RootFrame.NavigationFailed += RootFrame_NavigationFailed;

        // Ensure we don't initialize again
        phoneApplicationInitialized = true;

    }

    void App_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Debug.WriteLine("1");
        e.Cancel = true;
    }

在MainClass中,我在下面添加了代码,以确定它是否真的停止了

        public MainPage()
    {
        InitializeComponent();
        this.BackKeyPress += MainPage_BackKeyPress;
    }

    void MainPage_BackKeyPress(object sender, CancelEventArgs e)
    {
        Debug.WriteLine("2");
    }

令我失望的是,输出显示" 1 2"那么它有什么不可以抑制密钥执行呢?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,Windows Phone只能取消返回执行时有什么要回去,我的意思是任何PhoneApplicationPage。我想你按下第一页上的Go Back按钮,因此WP无需备份。在您的页面上添加按钮指向另一个页面,在此页面上按“返回”以查看是否禁止执行。