2013-07-19 76 views
0

嗨,我想通過棱鏡viewmodel實例化視圖,沒有運氣。顯示窗口,但沒有任何模塊區域被更新。在棱鏡中通過視圖模型實例化視圖

請參考下面的代碼片斷:如使用系統如下

public class ShellViewModel : ViewModelBase<IShellView> 
    { 
     public ShellViewModel([Dependency("ShellView")]IShellView view) 
      : base(view) 
     { 
     } 
    } 

public interface IShellView : IView 
    { 
     void ShowMessageInOutputWindow(string message); 
    } 

/// <summary> 
    /// Abstract base class for a ViewModel implementation. 
    /// </summary> 
    /// <typeparam name="TView">The type of the view. Do provide an interface as type and not the concrete type itself.</typeparam> 
    public abstract class ViewModelBase<TView> : ViewBase where TView : IView 
    { 
     /// <summary> 
     /// The view. 
     /// </summary> 
     private readonly TView view; 

     /// <summary> 
     /// Initializes a new instance of the <see cref="ViewModel&lt;TView&gt;"/> class and 
     /// attaches itself as <c>DataContext</c> to the view. 
     /// </summary> 
     /// <param name="view">The view.</param> 
     protected ViewModelBase(TView view) 
      : base(view) 
     { 
      this.view = view; 
     } 

     /// <summary> 
     /// Gets the associated view as specified view type. 
     /// </summary> 
     /// <remarks> 
     /// Use this property in a ViewModel class to avoid casting. 
     /// </remarks> 
     public TView View 
     { 
      get { return this.view; } 
     } 
    } 

public class Bootstrapper : UnityBootstrapper 
    { 
     private ShellViewModel shellViewModel; 

     protected override DependencyObject CreateShell() 
     { 
      // register shell types 
      var container = ServiceLocator.Current.GetInstance<IUnityContainer>(); 
      container.RegisterType<IShellView, ShellWindow>("ShellView"); 
      container.RegisterType<Object, ShellViewModel>("ShellViewModel"); 
      shellViewModel = container.Resolve<ShellViewModel>(); 
      this.Shell = shellViewModel.View as DependencyObject; 
      return this.Shell; 
      //return new ShellWindow(); 

     } 
... 

並且視圖模型定義; using System.ComponentModel;使用System.Threading的 ; using System.Windows.Threading;

/// 所有視圖模型 /// 公共抽象類ViewBase ///基類:INotifyPropertyChanging,INotifyPropertyChanged的 {/// /// 的觀點。 /// private readonly IView view;

/// <summary> 
/// Initializes a new instance of the <see cref="ViewModelBase"/> class and 
/// attaches itself as <c>DataContext</c> to the view. 
/// </summary> 
/// <param name="view">The view.</param> 
protected ViewBase(IView view) 
{ 
    if (view == null) 
    { 
     throw new ArgumentNullException("view"); 
    } 

    this.view = view; 

    // Check if the code is running within the WPF application model 
    if (SynchronizationContext.Current is DispatcherSynchronizationContext) 
    { 
     // Set DataContext of the view has to be delayed so that the ViewModel can initialize the internal data (e.g. Commands) 
     // before the view starts with DataBinding. 
     Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate 
     { 
      this.view.DataContext = this; 
     }); 
    } 
    else 
    { 
     // When the code runs outside of the WPF application model then we set the DataContext immediately. 
     this.view.DataContext = this; 
    } 
} 

#region INotifyPropertyChanging Members 

public event PropertyChangingEventHandler PropertyChanging; 

#endregion 

#region INotifyPropertyChanged Members 

public event PropertyChangedEventHandler PropertyChanged; 

#endregion 

#region Administrative Properties 

/// <summary> 
/// Whether the view model should ignore property-change events. 
/// </summary> 
public virtual bool IgnorePropertyChangeEvents { get; set; } 

#endregion 

#region Public Methods 

/// <summary> 
/// Raises the PropertyChanged event. 
/// </summary> 
/// <param name="propertyName">The name of the changed property.</param> 
public virtual void RaisePropertyChangedEvent(string propertyName) 
{ 
    // Exit if changes ignored 
    if (IgnorePropertyChangeEvents) return; 

    // Exit if no subscribers 
    if (PropertyChanged == null) return; 

    // Raise event 
    var e = new PropertyChangedEventArgs(propertyName); 
    PropertyChanged(this, e); 
} 

/// <summary> 
/// Raises the PropertyChanging event. 
/// </summary> 
/// <param name="propertyName">The name of the changing property.</param> 
public virtual void RaisePropertyChangingEvent(string propertyName) 
{ 
    // Exit if changes ignored 
    if (IgnorePropertyChangeEvents) return; 

    // Exit if no subscribers 
    if (PropertyChanging == null) return; 

    // Raise event 
    var e = new PropertyChangingEventArgs(propertyName); 
    PropertyChanging(this, e); 
} 

}

#endregion 


/// <summary> 
    /// Represents a view 
    /// </summary> 
    public interface IView 
    { 
     /// <summary> 
     /// Gets or sets the data context of the view. 
     /// </summary> 
     object DataContext { get; set; } 

     /// <summary> 
     /// Initializes the view 
     /// </summary> 
     void Initialize(); 
    } 
+0

隨着MVVM視圖模型不知道任何視圖。並且只有組成部分根知道IoC容器。你沒有顯示視圖的'DataContext'分配的位置,我在'ViewModelBase <>'中假設 - 我最近做了這個,並且我最終翻轉了它(即將ViewModel注入到View中),因爲IoC容器調用視圖的構造函數,得到一個沒有ViewModel的View,這似乎是你的問題? –

回答

0

這樣做雖然工作,但我看不出區別。

ShellWindow view = new ShellWindow(); 
      ShellViewModel viewModel = new ShellViewModel(view); 
      return viewModel.View as DependencyObject; 
0

當您在UnityContainer中使用名稱註冊類型時,還必須通過指定該名稱來解析它。您還必須使用您註冊的類型來解決問題。

container.RegisterType<Object, ShellViewModel>("ShellViewModel"); 
shellViewModel = container.Resolve<Object>("ShellViewModel") as ShellViewModel; 

您還應該爲ShellViewModel類定義一個接口,並使用該接口註冊/解析。然後

public interface ShellViewModel 
{ 
} 

你的引導程序是這樣的:

public class Bootstrapper : UnityBootstrapper 
{ 
    private IShellViewModel shellViewModel; 

    protected override DependencyObject CreateShell() 
    { 
     // register shell types 
     var container = ServiceLocator.Current.GetInstance<IUnityContainer>(); 
     container.RegisterType<IShellView, ShellWindow>("ShellView"); 
     container.RegisterType<IShellViewModel, ShellViewModel>("ShellViewModel"); 
     shellViewModel = container.Resolve<IShellViewModel>("ShellViewModel"); 
     this.Shell = shellViewModel.View as DependencyObject; 
     return this.Shell; 

    } 
} 
相關問題