0

私は持っていViewます:

internal partial class StartWindow : Window
{
    public StartWindow()
    {
        InitializeComponent();
    }

    [ImportingConstructor]
    public StartWindow(IStartWindowViewModel viewModel)
        :this()
    {
        ViewModel = viewModel;
    }

    public IStartWindowViewModel ViewModel { get; private set; }
}

そして適切なViewModel:

    [Export]
    internal class StartWindowViewModel : IStartWindowViewModel
    {
        public IEnumerable<Customer> Customers { get; set; }
    }

UPD (@Blachshma の回答に基づく): コードを使用して StartWindow ビューをアプリに挿入できません:

public class App
{
     [ImportingConstructor]
     public App(StartWindow window)
     {
          // Do whatever you need
     }

パラメーターなしのコンストラクターは App.g.cs によって必要とされるため:

internal partial class App : System.Windows.Application {

        /// <summary>
        /// Application Entry Point.
        /// </summary>
        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public static void Main() {
            TransparentNotification.App app = new TransparentNotification.App();
            app.Run();
        }
    }

app.csここで、viewviaでインスタンス化したいと思いますconstructor injection。私はそれのために何をすべきですか?

また、m looking for best practices forMEF /MVVM` ソリューション (このためのサンプル コードは素晴らしいアイデアです)。

PS .NET 4.5

4

1 に答える 1

0

MEFを使用してビューのインスタンスを取得する場合は、まずビューをエクスポートする必要があります。したがって[Export]、StartWindowクラスに属性を追加します。

[Export]
partial class StartWindow : Window
{
  ...

次に、AppクラスでImportingConstrutorを使用してインスタンスを取得します。

public class App
{
     [ImportingConstructor]
     public App(StartWindow window)
     {
          // Do whatever you need
     }

MVVMに関する優れた記事については、JoshSmithのブログをお勧めします

于 2013-01-31T21:18:45.443 に答える