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 certain conditions are true. So I have an event in StartScreen it checks if a directory exist or not but it throws me every time an error.
private void GoToPlayer_Click(object sender, RoutedEventArgs e)
{
if (Directory.Exists(this.main.workingDir + "/" + IDText.Text + "/Tracks")) // Error occurs here
{
this.main.Content = this.main.player; //here i switch between different ui forms
}
else
{
MessageBox.Text = "CD not found";
IDText.Text = "";
}
}
When it hit the else branch everything is fine but when the dir is available I get the following error message:
An exception of type 'System.InvalidOperationException' occurred in System.IO.FileSystem.dll but was not handled in user code
Additional information: Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.
Even if I commenting the code in the if branch out the error still comes.
I tried this:
private async void GoToPlayer_Click(object sender, RoutedEventArgs e)
{
await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => {
if (Directory.Exists(this.main.workingDir + "/" + IDText.Text + "/Tracks")) // Error occurs here
{
this.main.Content = this.main.player; //here i switch between different ui forms
}
else
{
MessageBox.Text = "CD not found";
IDText.Text = "";
}
});
}
Still the same error, as I understood this should be run asynchronous and wait until the code complete, but it doesn't seems so. I also tried bunch other stuff but still get the errors. I don't know how to fix that, could someone please explain why this is happening and how to fix that.