我已经将流行的UI库Mahapps与Avalon.Wizard控件集成在一起。
它可以很好地集成,但是我在Mahapps对话框中遇到问题。向导控件定义一个InitializeCommand来处理向导页面上的输入。
显然,初始化附加到视图的Dependency属性之前,会初始化InitializeCommand(DialogParticipation.Register)。
这会导致以下错误:
Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext.
here提供了一个重现问题的示例项目。
关于如何解决此问题的任何建议?
最佳答案
Xaml页面不是在initialize命令中创建的,因此您目前不能使用DialogCoordinator。
这是一个带有LoadedCommand的自定义接口,可以在ViewModel上实现该接口,并在后面的Xaml代码中调用它。
public interface IWizardPageLoadableViewModel
{
ICommand LoadedCommand { get; set; }
}
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);
}
}
和视图:
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); };
}
}
希望这可以帮助。
关于c# - Mahapps 1.3对话框和Avalon.Wizard,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39853326/