2016-04-07 4 views
0

アプリケーションに2つのViewModelとスクリーンコンダクターがあるとします。画面からスクリーンへのデータの受け渡し

public class ShellViewModel : Conductor<IScreen>, IShell 
{ 
    public ShellViewModel() 
    { 
     ShowConnectionScreen(); 
    } 
    public void ShowConnectionScreen() 
    { 
     ActivateItem(new ConnectionViewModel()); 
    } 

    public void ShowSetupScreen() 
    { 
     ActivateItem(new SetupViewModel()); 
    } 
} 

最初のViewModelは、起動時に表示され、いくつかのセットアップ情報とどこかへの接続を初期化Connectボタンが含まれています。

接続が正常に確立されたら、最初のViewModelを終了し、2番目のViewModelに接続に関する情報を表示します。それが失敗した場合、最初のViewModelは単にそれを表示し、ユーザーが再び接続を試みることを許可する必要があります。

したがって、実際の接続オブジェクトを最初のViewModelから2番目のViewModelおよびScreen Conductorに渡すと、成功したビューモデルが変更される必要があります。

これをCaliburn.Microでどのように実現できますか?

これは最初のビューモデルを移入することをconnection対象となり、明らかに問題

をこのタイプを作成し、3クラスのオブジェクト間の一般的なタイプを使用します。mvermefさんのコメント@説明するために

+1

明らかに問題のこのタイプを作成し、いずれかのコンストラクタで渡したり、財産作り、3クラスのオブジェクト間の一般的なタイプを使用3つのクラスすべての...?また、EventAggregatorを使用して、他のViewModelが受け入れて処理または無視するイベントをパブリッシュできます。 – mvermef

答えて

0

第2のビューモデルによって使用される。

public class Connection { 
    // props, methods, etc... 
} 

public class ConnectionViewModel : Screen 
{ 
    public Connection Connection { get; set; } 
    // establish connection 
    // can call (Parent as IConductor).DeactivateItem(this) 
    // after connection is established 
} 

ConnectionViewModel

内部のConnectionオブジェクトで何をしたいですかいずれかのコンストラクタに渡したり、それをすべての3つのクラス

public class ShellViewModel : Conductor<IScreen>, IShell 
{ 
    public Connection Connection { get; set; } 
    public ShellViewModel() 
    { 
     Connection = new Connection(); 
     ShowConnectionScreen(); 
    } 
    public void ShowConnectionScreen() 
    { 
     ActivateItem(new ConnectionViewModel(Connection)); 
    } 

    public void ShowSetupScreen() 
    { 
     ActivateItem(new SetupViewModel(Connection)); 
    } 
} 

の財産を作ります接続が確立されている場合は、(1)C経由で登録するonnectionViewModelのDeactivatedイベント(Screenのサブクラスを想定)。または、(2)EventAggregatorを使用して、接続が確立され、ShellViewModelでIHandleが実装されていれば、イベントを発生させることができます。 DeactivatedイベントハンドラまたはHandleメソッド内でShowSetupScreen()を呼び出すことができます。

オプション1:

// ShellViewModel 
public void ShowConnectionScreen() 
{ 
    var connectionVM = new ConnectionViewModel(); 
    connectionVM.Deactivated += ConnectionViewModel_Deactivated; 
    ActivateItem(); 
} 

private void Scheduler_Deactivated1(object sender, DeactivationEventArgs e) 
{ 
    ShowSetupScreen(); 
} 

オプション2:

public class ShellViewModel : Conductor<IScreen>, 
    IShell, IHandle<string> 
{ 
    private readonly IEventAggregator _eventAggregator; 

    public ShellViewModel(IEventAggregator eventAggregator) 
    { 
     _eventAggregator = eventAggregator; 
     _eventAggregator.Subscribe(this); 
    } 

    // from IHandle<string>. you can create a custom object to represent this event 
    public void Handle(string message) 
    { 
     if (message.Equals("connection.successful")) 
     { 
      ShowSetupScreen(); 
     } 
    } 
} 

public class ConnectionViewModel : Screen 
{ 
    private readonly IEventAggregator _eventAggregator; 
    public ConnectionViewModel(IEventAggregator eventAggregator) 
    { 
     _eventAggregator = eventAggregator; 
    } 

    // call _eventAggregator.PublishOnUIThread("connection.successful"); 
} 
関連する問題