Closed qusma closed 2 years ago
@qusma It would be help if you can create a simple app which reproduces this issue. Thx
Here you go: https://filebin.net/ewjesvzqjx7dpn6k
I managed to narrow it down a bit, the issue seems to be related to showing a splash screen first before the main window. (It used to work in the past on .NET Framework btw.)
Hello everyone! I got the same error:
System.InvalidOperationException HResult=0x80131509 Message = Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext. Source = MahApps.Metro Трассировка стека: at MahApps.Metro.Controls.Dialogs.DialogCoordinator.GetMetroWindow(Object context) at MahApps.Metro.Controls.Dialogs.DialogCoordinator.ShowMessageAsync(Object context, String title, String message, MessageDialogStyle style, MetroDialogSettings settings) at XXX.ViewModels.XXXViewModel.
d__91.MoveNext() in \XXX\ViewModels\XXXViewModel.cs:line 383
View.xaml.cs & DataContext XAML Method There are no errors in this place, the dialog is displayed But if Icall the method here, an exception appears Exception
The specified View is created from the ViewModel of another window. If I don't call the method from the constructor, the dialog is displayed correctly. It is not clear why the DataContext is lost if the same method is called from the constructor.
I also checked the assumption of @qusma -- SplashScreen, which I have added to the application resources, does not affect the operation of the dialog box
Hi @argentmize
Maybe you cannot call this in the constructor because the VM is not registered yet.
I think this is going on:
DataContext
Bindings
are set nowAs you see step 3 should have been done after step 4.
I see two solutions:
DialogCoordinator
here and instead search for the acitve window to show it (I know this breaks the MVVM
pattern a bit, but I am using this in such situations)Solution 2 explained:
var window = App.Current.Windows.OfType<MetroWindow>().FirstOrDefault(x => x.IsActive);
if (window is not null)
{
await window.ShowMessageAsync("Error", "My Message");
}
else
{
MessageBox.Show("FALLBACK DIALOG IF THE ABOVE FAILED");
}
Happy coding Tim
Hi @timunie!
Thank you for your advice!
I tried the second solution - the dialog box is shown, but only on the previous window. That is, I have a main window (1) with a button that opens the next window (2). Depending on the choice in the second window, another window is created (3), where the dialog box should be displayed. But with your option, the following sequence turns out: there is no dialog, an empty window (3) is created (because the bindings did not work). I close the window and see an error dialog 🤣 I decided to look further for now, your option will work in another project, thx.
I think something is really going on with the initialization order, but I have no guesses and no relevant knowledge. I expected that if the documentation is so sparse, then everything should be simple. I was wrong 😀
Also I tried another solution.
Good luck Augustyn
Hi @argentmize ,
well I think you need some way to detect the window you want to show the message. My assumption was that you may want to show it on the active window. If not, you may test for the Title
-Property or the Tag
-Property or you replace .OfType<MetroWindow>()
with .OfType<MyDerivedWindowClass>()
.
Happy coding Tim
Hi @timunie
Understood, thanks. So far, I've chosen to skip the DialogCoordinator and just pass the window object reference through the VM constructor (without MVVM - https://mahapps.com/docs/dialogs/message-dialog)
public partial class XXXView : MetroWindow
{
public XXXView(int taskId)
{
InitializeComponent();
DataContext = new XXXViewModel(taskId, this);
}
}
//...
public async void ShowDialog(string title, string text)
{
await _current.ShowMessageAsync(title, text);
}
public XXXViewModel(int taskId, MetroWindow curr)
{
_current = curr; // MetroWindow
try { }
catch { ShowDialog(Title, Text); }
}
This works as planned. Thanks for the help
Good luck Augustyn
Describe the bug
I have a standard MVVM setup for dialogs. XAML:
Then in the window constructor I pass the DialogCoordinator.Instance:
Attempting to create a dialog from the ViewModel, like this:
Results in the overlay coming up (the window gets grayed out), no dialog appearing, and a few moments later an exception thrown:
Same thing happens trying to create the dialog directly from the window like this:
Environment
Screenshots