"Windows 10 App development" uses shortcut access keys

Source: Internet
Author: User

Although the UWP is primarily touch-oriented, the Windows device is a universal tool that can be played with keyboards without using a pen. So it's nice to get a quick access key to some elements on the application interface. To be unified with the various applications on Windows, the shortcut access keys for UWP apps continue to be traditional, and the activation method is to press the ALT key first, and then press the access key without releasing the ALT key.

Let's start with an example to perform.

The following XAML declares a button and sets an access key.

<Content= "Update"  Click= "OnUpdate"  AccessKey= " U "/>

Access keys are used for buttons and menu items, so the click event is automatically raised when the access key is activated when the button is used.

Let's deal with the Click event and Play a dialog box.

        Private Async void OnUpdate (object  sender, RoutedEventArgs e)        {            "Update button is activated. ";             await Messagedlg.showasync ();        }

Now this access key can be used. Run the program and press ALT + U to see the effect as shown.

The button is special, and the access key is automatically associated with the Click event when it is activated, but not for normal UI elements. Like what

<Fill= "Red"  Height= "  Width= " HorizontalAlignment= "Left"  AccessKey= "R"/>

For the above rectangle, pressing ALT + R system is not able to judge the behavior of its completion, if I really want this rectangle can also respond to access keys, how to do? This is the time to handle the Accesskeyinvoked event, when the access key is correctly identified, this event is raised, we can handle the event, and then write our own code.

For example, I am here, the same popup dialog box.

<Fill= "Red" ... AccessKey= "R"   accesskeyinvoked= "rectangle_accesskeyinvoked"/> 

The following is the event handling code.

        Private Async void rectangle_accesskeyinvoked (UIElement sender, Accesskeyinvokedeventargs args)        {            " This is a red rectangle."} ";             await Messagedlg.showasync ();        }

When you run the program, press ALT + R and the event is raised. Such as

To display an access key hint, just press the ALT key, as in.

A big partner would say, if I don't want to use the system default access key hint, I want to handle it myself.

Of course you can, first of all, you have to execute this line of code inside the code.

false;

In this way, the default display prompt is disabled.

Then, you have to handle two events:

accesskeydisplayrequested -This event is raised when you want to display an access key prompt, which allows you to add your processing code and display an access key hint to the user.

accesskeydisplaydismissed-thrown when prompted to close (disappear), in which case you will use your code to hide the access key hint.

Here, let's change the rectangle above.

            <RectangleFill= "Red"Height= "+"Width= "+"HorizontalAlignment= "Left"AccessKey= "R"accesskeyinvoked= "rectangle_accesskeyinvoked"Margin= "0,13"accesskeydisplayrequested= "Onaccesskeydisplayreq"accesskeydisplaydismissed= "Onaccesskeydisplaydism">                <Rectangle.contextflyout>                    <flyoutPlacement= "Bottom">                        <flyout.content>                            <TextBlockFontSize= "+"Foreground= "Red"/>                        </flyout.content>                    </flyout>                </Rectangle.contextflyout>            </Rectangle>

I chose a flyout class to display the hints.

The following is the processing event code.

        Private voidOnaccesskeydisplayreq (UIElement sender, Accesskeydisplayrequestedeventargs args) {FrameworkElement Ele= Sender asFrameworkElement; Flyout Fly= Ele. Contextflyout asflyout; TextBlock TB= Fly. Content asTextBlock; Tb. Text= $"Please press "ALT + {ele". AccessKey} "key"; Fly.        Showat (ele); }        Private voidonaccesskeydisplaydism (UIElement sender, Accesskeydisplaydismissedeventargs args) {frameworkelemen T Ele= Sender asFrameworkElement; flyout FL= Ele. Contextflyout asflyout; Fl.        Hide (); }

OK, now run again, and then click the Alt key. Effect.

OK, today's topic will talk about here, the old week is also hungry, it is time to fill the belly.

The sample code for this article

"Windows 10 App development" uses shortcut access keys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.