[C# UWP] FileOpenPicker opens twice with keyboard accelerators

SpectraCoder 141 Reputation points
2020-08-17T08:48:14.897+00:00

I would like to add keyboard shortcuts to some buttons.
But for some reason the keyboard accelerator triggers the event twice.
When I just click the button with the mouse, it works as expected.

<MenuBar>
        <MenuBarItem Title="File">
            <MenuFlyoutItem Text="Open file..." Click="OnOpen" Icon="OpenFile">
                <MenuFlyoutItem.KeyboardAccelerators>
                    <KeyboardAccelerator Key="O" Modifiers="Control"/>
                </MenuFlyoutItem.KeyboardAccelerators>
        </MenuFlyoutItem>
   </MenuBarItem>
</MenuBar>

FileLoader.LoadFile here shows the FileOpenPicker:

        private async void OnOpen(object sender, RoutedEventArgs e)
        {
              if (!Busy)
             {
                       var loadedFile = await FileLoader.LoadFile(".txt", PickerLocationId.Desktop);

                       if(loadedFile ! null) //for some reason I can't type the doesn't equal operator on this forum without getting access denied errors.
                       {
                             await FileLoader.LoadTextFile(loadedFile);
                        }
               }

        }

The same problem is happening with a FileSavePicker elsewhere in my code.
How can I prevent this from happening?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2020-08-19T09:34:43.783+00:00

    Hello,

    Welcome to Microsoft Q&A,

    For the testing, your code is correct and the issue does not reproduce in target version 1903. And above problem has fixed in 1903, please try to modify the target version and rebuild again.

    Thanks
    Nico Zhu

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.