Perform synchronous operation on Ui thread

后端 未结 2 1670
春和景丽
春和景丽 2020-12-11 22:48

I am trying develop a Windows App and run into issues. I have a MainPage.xaml and 2 others StartScreen.xaml and Player.xaml. I am switching content of the MainPage if certai

2条回答
  •  渐次进展
    2020-12-11 23:10

    As the exception says, you are not allowed to call Directory.Exists synchronously in the UI thread. Putting the whole code block in a Dispatcher action still calls it in the UI thread.

    In a UWP app you would usually use the StorageFolder.TryGetItemAsync method to check if a file or folder exists:

    private async void GoToPlayer_Click(object sender, RoutedEventArgs e)
    {
        var folder = await StorageFolder.GetFolderFromPathAsync(main.workingDir);
    
        if ((folder = await folder.TryGetItemAsync(IDText.Text) as StorageFolder) != null &&
            (folder = await folder.TryGetItemAsync("Tracks") as StorageFolder) != null)
        {
            ...
        }
    }
    

    Note that you may still get an UnauthorizedAccessException when the application is not allowed to access main.workingDir.

提交回复
热议问题