2016-10-04 85 views
2

人気のあるUIライブラリMahappsAvalon.Wizardコントロールに統合しました。Mahapps 1.3ダイアログとAvalon.Wizard

これはうまく統合されていますが、私はMahappsダイアログに問題があります。ウィザードコントロールは、ウィザードページでの入力を処理するためにInitializeCommandを定義します。

明らかにInitializeCommandは、ビューに関連付けられた依存関係プロパティが初期化される(DialogParticipation.Register)前にトリガーされます。

これは、次のエラーが発生:問題はhere可能です再現

Context is not registered. Consider using DialogParticipation.Register in XAML to bind in the DataContext. 

サンプルプロジェクトを。

これを修正する方法についてのご意見はありますか?

+1

ダイアログを表示するDialogCoordinatorを使用することはできませんので、ページのXAMLは、まだ、InitializeCommandで作成されません。私はXamlのLoadedイベントで実行されるカスタムインターフェイスを使ってサンプルでPullRequestを作成しました。 – punker76

答えて

3

Xamlはinitializeコマンドで作成されないため、この時点でDialogCoordinatorを使用することはできません。

ここには、ViewModelで実装してXamlコードの背後で呼び出すことができるLoadedCommandを備えたカスタムインターフェイスがあります。

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); }; 
    } 
} 

・ホープ、このことができます。

enter image description here

enter image description here

+0

それは動作します!ありがとうございました:) – dna2

+0

も私のために働いた。さらに、[ここ](https://msdn.microsoft.com/en-us/magazine/dd419663.aspx)に記述されているように 'RelayCommand'を定義する必要があり、' LastPageViewModel _lastPageViewModel = new LastPageViewModel(); 'を'DialogParticipation.SetRegister'を使う前に' DataContext'を呼び出してください。 –

関連する問題