Caliburn.Micro,如何从视图中访问实际使用的ViewModel

5

我希望能够从视图(代码后台)访问当前使用的实际视图模型。在引导程序中,我将视图模型设置为每个请求,因此我无法使用IoC.Get<..ViewModel>();(我也不想更改此行为)。

基本上,我正在寻找与屏幕的GetView相当的东西,但是反过来。

2个回答

7

DataContext会给您当前应用于视图DataContext的ViewModel。

// Get you the object of ViewModel.
var viewModelInstance = DataContext;

// Or typecast to exact instance what you intend to use.
MyViewModel vm = DataContext as MyViewModel;

1
啊,是的,当然。完全忘记了那个属性。谢谢! - The Cookies Dog

1
请记住,DataContextView加载后将被赋值。例如,您可以在View的loaded事件中访问它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接