0

App.xaml.cs に存在する文字列 UserName があり、これを ViewModel に渡してコントロールに入力する必要があります。これを達成するための最良の方法は何ですか?

4

2 に答える 2

1

コントロール リソースを使用して ViewModel インスタンスにアクセスします。次に、ViewModel で定義された public メソッドを使用して、パラメーターを渡すことができます。

((ViewModel1)this.Resources["ViewModel1"]).Test(p);

Test は、パラメーターを受け取る ViewModel の public メソッドです。

于 2013-02-25T06:06:29.300 に答える
0

値を含むメッセージ (MvvmLight Messenger クラスなど) を送信すると、ViewModel がそれを受信するように登録されます。

App.xaml.cs で:

Messenger.Default.Send<string>(Username);

ViewModel のコンストラクターで:

Messenger.Default.Register<string>(this, m => 
    {
        this.Username = m; // or whatever you need to do with the value
    });
于 2013-02-25T10:05:18.757 に答える