Caliburn Micro ActivateItem不更新绑定

Caliburn Micro ActivateItem不更新绑定

问题描述:

考虑一个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()

出现这种情况因为... 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); 
    }; 
}