问题
I've integrated the popular UI library Mahapps with the Avalon.Wizard control.
It integrates nicely, but I have an issue with the Mahapps dialogs. The Wizard control defines an InitializeCommand to handle the entering on a wizard page.
Apparently the InitializeCommand is triggered before the Dependency Property attached to the View is initialized (DialogParticipation.Register).
This cause the following error:
Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.
A sample project that reproduce the issue is available here.
Any suggestion on how to fix this?
回答1:
The page Xaml isn't created at the initialize command, so you can't use the DialogCoordinator at this point.
Here is a custom interface with a LoadedCommand which can you implement at the ViewModel and call it at the Xaml code behind.
public interface IWizardPageLoadableViewModel
{
ICommand LoadedCommand { get; set; }
}
The ViewModel:
public class LastPageViewModel : WizardPageViewModelBase, IWizardPageLoadableViewModel
{
public LastPageViewModel()
{
Header = "Last Page";
Subtitle = "This is a test project for Mahapps and Avalon.Wizard";
InitializeCommand = new RelayCommand<object>(ExecuteInitialize);
LoadedCommand = new RelayCommand<object>(ExecuteLoaded);
}
public ICommand LoadedCommand { get; set; }
private async void ExecuteInitialize(object parameter)
{
// The Xaml is not created here! so you can't use the DialogCoordinator here.
}
private async void ExecuteLoaded(object parameter)
{
var dialog = DialogCoordinator.Instance;
var settings = new MetroDialogSettings()
{
ColorScheme = MetroDialogColorScheme.Accented
};
await dialog.ShowMessageAsync(this, "Hello World", "This dialog is triggered from Avalon.Wizard LoadedCommand", MessageDialogStyle.Affirmative, settings);
}
}
And the View:
public partial class LastPageView : UserControl
{
public LastPageView()
{
InitializeComponent();
this.Loaded += (sender, args) =>
{
DialogParticipation.SetRegister(this, this.DataContext);
((IWizardPageLoadableViewModel) this.DataContext).LoadedCommand.Execute(this);
};
// if using DialogParticipation on Windows which open / close frequently you will get a
// memory leak unless you unregister. The easiest way to do this is in your Closing/ Unloaded
// event, as so:
//
// DialogParticipation.SetRegister(this, null);
this.Unloaded += (sender, args) => { DialogParticipation.SetRegister(this, null); };
}
}
Hope this helps.
来源:https://stackoverflow.com/questions/39853326/mahapps-1-3-dialogs-and-avalon-wizard