0

こんにちは、運の悪いプリズムでビューモデルを介してビューをインスタンス化しようとしています。ウィンドウは表示されますが、モジュール領域は更新されません。

以下のコード スニペットを参照してください。

 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();

        }
...

そして、ビューモデルは次のように定義されています

 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; }
        }
    }

システムを使用する; System.ComponentModel の使用; System.Threading の使用; System.Windows.Threading の使用;

/// /// すべてのビュー モデルの基本クラス /// public abstract class ViewBase : INotifyPropertyChanging, INotifyPropertyChanged { /// /// ビュー。/// 読み取り専用のプライベート IView ビュー。

/// <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();
    }
4

2 に答える 2

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;

    }
}
于 2013-07-19T21:16:26.347 に答える