Mahapps 1.3 dialogs and Avalon.Wizard

六月ゝ 毕业季﹏ 提交于 2019-12-06 14:32:46

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!