I have this class that allows only one instance per window stolen from other stackoverflow question:
public static class WindowManager
{
public static T GetWindow<T>()
{
var t = Application.Current.Windows.OfType<T>().FirstOrDefault();
if (t == null)
{
t = (T)Activator.CreateInstance(typeof(T));
}
return t;
}
public static T CreateOrFocusWindow<T>()
{
var t = GetWindow<T>();
if (t is Window)
{
var window = t as Window;
if (window.Visibility != Visibility.Visible)
window.Show();
if (window.WindowState == WindowState.Minimized)
window.WindowState = WindowState.Normal;
window.Focus();
}
return t;
}
}
And now I want to open new window:
private async void Button_OnClick(object sender, RoutedEventArgs e)
{
LoadingOn(); //enables progress ring etc
await Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
WindowManager.CreateOrFocusWindow<Window1>();
});
});
LoadingOff(); //disables progress ring etc
}
But the problem is that UI is locked anyway (constructor of window contains long operation). I don't have any ideas how to bypass it.
There might be a way to simply first create instance of the window, and then call long operation not from constructor, but from OnLoading
event, but its completely different approach that requires adding loading spinner to every new window. I just want to open new window when data is ready without nuking the way my code currently works. It looks like its impossible, but I'm not master of C# and WPF yet.