Type to search in Windows 8

In het startscherm kan je gaan typen om te gaan zoeken in een applicatie. Dit gedrag is ook heel eenvoudig in je applicatie te realiseren, maar er zit een maar aan. De meest voor de handliggende oplossing op keydown van het core window het searchpane openen:

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
      SearchPane.GetForCurrentView().Show();
}

 Echter zal je zien met deze oplossing dat je altijd de eerste toetsaanslag mist in de seearch query. Dit is onhandig. Best is om de event handler iets uit te breiden en zelf de eerste toetsenbord aanslag af te handelen:

void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
        {

            if ((int)args.VirtualKey >= 48 && (int)args.VirtualKey <= 90)
            {
                string text = string.Empty;

                switch (args.VirtualKey)
                {
                    case Windows.System.VirtualKey.Number0:
                        text = "0";
                        break;
                    case Windows.System.VirtualKey.Number1:
                        text = "1";
                        break;
                    case Windows.System.VirtualKey.Number2:
                        text = "2";
                        break;
                    case Windows.System.VirtualKey.Number3:
                        text = "3";
                        break;
                    case Windows.System.VirtualKey.Number4:
                        text = "4";
                        break;
                    case Windows.System.VirtualKey.Number5:
                        text = "5";
                        break;
                    case Windows.System.VirtualKey.Number6:
                        text = "6";
                        break;
                    case Windows.System.VirtualKey.Number7:
                        text = "7";
                        break;
                    case Windows.System.VirtualKey.Number8:
                        text = "8";
                        break;
                    case Windows.System.VirtualKey.Number9:
                        text = "9";
                        break;
                    case Windows.System.VirtualKey.A:
                    case Windows.System.VirtualKey.B:
                    case Windows.System.VirtualKey.C:
                    case Windows.System.VirtualKey.D:
                    case Windows.System.VirtualKey.E:
                    case Windows.System.VirtualKey.F:
                    case Windows.System.VirtualKey.G:
                    case Windows.System.VirtualKey.H:
                    case Windows.System.VirtualKey.I:
                    case Windows.System.VirtualKey.J:
                    case Windows.System.VirtualKey.K:
                    case Windows.System.VirtualKey.L:
                    case Windows.System.VirtualKey.M:
                    case Windows.System.VirtualKey.N:
                    case Windows.System.VirtualKey.O:
                    case Windows.System.VirtualKey.P:
                    case Windows.System.VirtualKey.Q:
                    case Windows.System.VirtualKey.R:
                    case Windows.System.VirtualKey.S:
                    case Windows.System.VirtualKey.T:
                    case Windows.System.VirtualKey.U:
                    case Windows.System.VirtualKey.V:
                    case Windows.System.VirtualKey.W:
                    case Windows.System.VirtualKey.X:
                    case Windows.System.VirtualKey.Y:
                    case Windows.System.VirtualKey.Z:
                        text = args.VirtualKey.ToString().ToLower();
                        break;
                }


                if ((Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.Shift) & Windows.UI.Core.CoreVirtualKeyStates.Down) == Windows.UI.Core.CoreVirtualKeyStates.Down ||
                    (Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.CapitalLock) & Windows.UI.Core.CoreVirtualKeyStates.Locked) == Windows.UI.Core.CoreVirtualKeyStates.Locked)
                {
                    text = text.ToUpper();
                }

                SearchPane.GetForCurrentView().Show(text);
                Frame.Navigate(typeof(SearchPageView), text);
            }
        }
  •   jeej skype till 22h today..
  •   @VSTS @awscloud @chef @Docker @GooglePlay what are the 10 highest rated widgets?
  •   RT @wpschaub: Over 500 extensions in the VSTS/TFS marketplace https://t.co/pH64ZFRi2J
  •   @mitrik what is difference between code -&gt; files -&gt; history and code -&gt; commits? looks as same? https://t.co/NnA8lk1pTe
  •   @LocalJoost 😵
  •   the moment you need to install ms project....
  •   RT @coolcsh: And we have updated Visual Studio 2017 w/15.3 to for .NET Core 2, https://t.co/2y7Qj07QrB Core 2, .NET Standard 2: https://t.c…
  •   @JenMsft do you know what changed in the latest version of the mail app? the whats new web page is not beeing updated for long time..
  •   new windows sdk! https://t.co/qOlWLICyIB #uwp #uwpdev
  •   @tfsbuck this way doesnt take in consideration it can be set back to "new" because some blocking issues?