relationship initializeViewModel1

以上是簡單的關係(不要問命名問題…)

 

當面對在View中為ViewModel提供初始值時,即”Initialize ViewModel”被剔選的話,View的base class中的初始化函數InitializeViewModel會有一段由uFrame產生的邏輯被執行。

    protected override void InitializeViewModel(uFrame.MVVM.ViewModel model) {
        base.InitializeViewModel(model);
        // NOTE: this method is only invoked if the 'Initialize ViewModel' is checked in the inspector.
        // var vm = model as DebugManViewModel;
        // This method is invoked when applying the data from the inspector to the viewmodel.  Add any view-specific customizations here.
        var debugmanview = ((DebugManViewModel)model);
        debugmanview.Fox = this._Fox == null ? null :  ViewService.FetchViewModel(this._Fox) as DebugFoxViewModel;
    }

目的是抽取View裡面的ViewModel放進自己的Property裡面。

 

如果我們將DebugFoxView與DebugManView以NestedPrefab形式儲存,

而需要在其他View邏輯使用publish InstantiateViewCommand去產生這個View,
而我們又想保持上圖的初始化結果,便需要於Command被Publish後為View(Result)執行一次以下邏輯。

        var debugMan = DebugManController.CreateDebugMan();
        var cmd = new InstantiateViewCommand() {
            Prefab = Resources.Load<GameObject>("DebugManView"),
            ViewModelObject = debugMan
        };

        this.Publish(cmd );
        ((DebugManView)cmd.Result)._Fox.ViewModelObject = debugMan.Fox;