Code: Select all
public MyViewModel(IWindowService windowService)
{
this.windowService = windowService;
}
public void Close()
{
this.OnRequestClose?.Invoke(this, new EventArgs());
}
Code: Select all
public void Open(IViewModel viewModel)
{
Window win = new(viewModel); //This is a window that inherits from Window but has a parameter to take a datacontext
viewModel.OnRequestClose += (s, e) =>
{
win.Close();
};
win.Show();
}
public void ShowMessageDialog(string title, string message)
{
MessageDialogViewModel dialog = new(message);
this.ShowDialog(title, dialog);
}
public void ShowDialog(string title, T viewModel) where T : ICloseable
{
Window window = new()
{
Title = title,
Content = viewModel,
};
viewModel.OnRequestClose += (s, e) =>
{
window.Close();
};
window.ShowDialog();
}
< /code>
Nun, dies funktioniert alles, außer in dem Fall, in dem ich einen Aktionsdelegierten habe, der aufgerufen wird, z. Diese Benachrichtigung von SignalR (im ViewModel): < /p>
connection.On("Completed", () =>
{
this.windowService.ShowMessageDialog("Completed", "Completed");
this.Close();
});
< /code>
, was die folgenden Fehler wirft: < /p>
System.InvalidOperationException: 'The calling thread must be STA, because many UI components require this.'
Code: Select all
System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.'