0

我有一个驻留在 App.xaml.cs 中的字符串 UserName,我需要将其带到 ViewModel 以填充控件。实现这一目标的最佳方法是什么?

4

2 回答 2

1

您最常使用控制资源来访问 ViewModel 实例。然后通过在 ViewModel 中定义的公共方法,您可以将参数传递给它。

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

Test 是 ViewModel 中接收参数的公共方法。

于 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 回答