UWP C# Number.Box key press.

VoyTec 671 Reputation points
2021-10-19T18:27:31.553+00:00

Greetings dear M Community,

I have this code for hitting ENTER in a number box (from Microsoft.UI.Xaml).
And it has to be pressed twice until he gets wanted values - for the first time it equals a value: NaN.

Why I am facing such problem? What is the solution for this?

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2021-10-20T09:10:07.963+00:00

    Hello,
    Welcome to Microsoft Q&A!

    Please check the following code, and update your check method with double type parameter, and use newValue to replace Number Value property.

    private void NumberBox_KeyDown(object sender, KeyRoutedEventArgs e)  
    {  
        if (e.Key == Windows.System.VirtualKey.Enter)  
        {  
            Task.Run(() =>  
            {             
                autoResetEvent.WaitOne();  
                check(newValue);                    
              
            });  
      
        }  
    }  
      
    private async void check(double roomvalue)  
    {  
      
        StorageFolder folderSP = await KnownFolders.DocumentsLibrary.GetFolderAsync("Św. Puszcza App");  
        try { StorageFolder folderDATE = await folderSP.GetFolderAsync(date_string); }  
        catch { Windows.Storage.StorageFolder folderDATE = await folderSP.CreateFolderAsync(date_string, CreationCollisionOption.ReplaceExisting); }  
        StorageFolder newfolderDATE = await folderSP.GetFolderAsync(date_string);  
        try { StorageFile file = await newfolderDATE.GetFileAsync(roomvalue + ".dat"); }  
        catch { Windows.Storage.StorageFile file = await newfolderDATE.CreateFileAsync(roomvalue + ".dat", CreationCollisionOption.ReplaceExisting); }  
        Windows.Storage.StorageFile fileROOM = await newfoldeRDATE.GetFileAsync(roomvalue + ".dat");  
        string text = await Windows.Storage.FileIO.ReadTextAsync(filerROOM);  
      
      
    }  
    private double newValue;  
    private double oldValue = -1;  
    AutoResetEvent autoResetEvent = new AutoResetEvent(false);  
    private void MyBox_ValueChanged(Microsoft.UI.Xaml.Controls.NumberBox sender, Microsoft.UI.Xaml.Controls.NumberBoxValueChangedEventArgs args)  
    {  
      
        if (args.NewValue != double.NaN)  
        {  
            newValue = args.NewValue;  
            autoResetEvent.Set();  
        }  
      
    }  
    

    Xaml

       <uicontrols:NumberBox   
           Height="44"  
           KeyDown="NumberBox_KeyDown"            
           ValueChanged="MyBox_ValueChanged"  
           Value="NaN" />  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.