2017-08-01 103 views
0

考慮一個ShellViewModel : Conductor<T>.Collection.OneActive其中我有2個通過PropertyInjection注入的ViewModel。所以這些viewmodel總是有相同的實例。Caliburn Micro ActivateItem不更新綁定

public SomeViewModel SomeViewModel {get; set;}  
public AnotherViewModel AnotherViewModel {get; set;} 

SomeViewModel具有結合的文本到一個屬性(與NotifyPropertyChanged)和具有附接的轉換器一個TextBlock。

  • 我第一次打電話ActivateItem(someViewModel)我會輸入在SomeView.xaml中定義的轉換器。
  • 如果我再撥打ActivateItem(AnotherViewModel),然後再撥ActivateItem(SomeViewModel)我不會輸入這個轉換器。

我希望調用ActivateItem總是會更新綁定並再次執行轉換器,或者我做錯了什麼?

我知道沒有任何改變到屬性本身,NotifyPropertyChanged不被調用。但我希望可視化一個用戶控件將刷新綁定。 據我知道這是老派的WPF應用程序發生了什麼不Caliburn.Micro

有沒有一種辦法,以確保這些綁定刷新無:

  • 中的OnActivate()調用Refresh() SomeViewModel
  • 發佈上,將在SomeViewModel處理的EventAgregator的事件,將調用Refresh()

回答

0

出現這種情況因爲... e視圖緩存:A Conductor<T>Screen,並且ScreenViewAware。該類用於緩存視圖實例:您會注意到,在您的視圖模型之間來回導航時,第一個視圖中ScrollViewer的位置將保持不變,即使它未綁定到任何屬性。

您可以覆蓋你的視圖模型的GetView(object)方法,像這樣:

public override object GetView(object context = null) 
{ 
    return null; 
} 

這將從根本上禁用視圖緩存。

或者,您可以修改視圖位置的代碼以適合您的需求。只需將其添加到引導程序啓動:

private void CustomizeViewLocation() 
{ 
    var log = LogManager.GetLog(typeof(ViewLocator)); 

    ViewLocator.LocateForModel = (model, displayLocation, context) => { 

     // Either remove the IViewAware handling to completely disable 
     // view caching... 

     var viewAware = model as IViewAware; 
     if (viewAware != null) 
     { 
      var view = viewAware.GetView(context) as UIElement; 
      if (view != null) 
      { 
       // Or add this custom code to always refresh... 

       var propertyChangedBase = model as PropertyChangedBase; 
       if (propertyChangedBase != null) 
       { 
        propertyChangedBase.Refresh(); 
       } 

       // End custom code. 

       var windowCheck = view as Window; 
       if (windowCheck == null || (!windowCheck.IsLoaded && !(new WindowInteropHelper(windowCheck).Handle == IntPtr.Zero))) 
       { 
        log.Info("Using cached view for {0}.", model); 
        return view; 
       } 
      } 
     } 

     return ViewLocator.LocateForModelType(model.GetType(), displayLocation, context); 
    }; 
} 
相關問題