开发者

c# windows mobile standard - Back button won't close the app

开发者 https://www.devze.com 2022-12-08 15:47 出处:网络
The background of the app is still there when i push the back button. Does anyone know what i can do, to prevent开发者_JS百科 this?

The background of the app is still there when i push the back button. Does anyone know what i can do, to prevent开发者_JS百科 this? If i use Application.Exit(), it works.

thanks in advance love stackoverflow :)


The back button is a Windows Mobile Standard (smartphone) feature that is typically not present in Windows Mobile Professional or Classic (pocket pc) devices.

At the native OS level the following MSDN article titled "The Back Button and Other Interesting Buttons" describes the default behavior and how to override or change it.

Within the .NET CF environment take a look at the "How to: Override the Smartphone Back Key" article available on MSDN. For example, within a form class you should be able to do something like the following.

// Connect an event handler to the KeyPress event
this.KeyPress += new KeyPressEventHandler(OnKeyPress);

private void OnKeyPress(object sender, KeyPressEventArgs ke)
{
    // Determine if ESC key value is pressed.
    if (ke.KeyChar == (Char)Keys.Escape)
    {
        // Handle the event to provide functionality.
        ke.Handled = true;

        // Add your event handling code here.
        MessageBox.Show("Back key was pressed.");
    }
}


I'm not sure exactly what you mean by the 'back button', but the default behavior in windows mobile for all apps is that the "X" button on the app that would be the "close" button in standard Windows, actually behaves like "minimize" and moves the app to the background.

To really "close" the application, you would normally go into the windows mobile task manager and end the application.


You can use the native function AllKeys and set the Form.KeyPreview to true to receive KeyDown events when the user presses hardware keys. Add a KeyDown event handler to your form, put a breakpoint in the KeyDown handler and press the "back" button and examine the value of KeyEventArgs.KeyCode. You can set KeyEventArgs.Handled to true to prevent the hardware keypress from being process by Windows Mobile.

Here is the PInvoke information:

[DllImport("coredll.dll", SetLastError = true)]
static extern bool AllKeys(bool bAllKeys);

Call AllKeys(true) to receive KeyDown events for hardware keys. Call AllKeys(false) to stop receiving KeyDown events for hardware keys. There is no official documentation for this function, but it has existed since Pocket PC 2003. See here for more information.


In Windows Mobile, you normally do not end processes. When you close the application with the [X] button, or when you go to the home screen, it is common to leave the app running.

An app on Windows Mobile does not take very much more memory, since it is not copied from harddisk into ram. It just stays on the same RAM spot, only now it is "active". Naturally your app should behave good, by taking as less resources as possible when it does not have focus.

If you do not want this behavior, you could exit the application ( Application.Exit() ), when your form looses focus. You can catch this easily with the LostFocus event.

Could it be that the "back-button" on my device is the one with the red phone printed on?

0

精彩评论

暂无评论...
验证码 换一张
取 消