在使用 Autofac 容器并注册了 VM 的应用程序中,我需要在我只有视图模型类型的情况下分配 DataContext。
MainViewModel 调用NavigationService:
await NavigationService.NavigateToAsync<UpdateViewModel>();
在我的服务课程中,如何做(这很好用):
private async Task InternalNavigateToAsync(Type viewModelType, object parameter)
{
var bootStrapper = new BootStrapper();
var container = bootStrapper.BootStrap();
Window window = CreateWindow(viewModelType, parameter);
//this works fine
if (viewModelType.Name == "MainViewModel")
{
window.DataContext = container.Resolve<MainViewModel>();
}
if (viewModelType.Name == "UpdateViewModel")
{
window.DataContext = container.Resolve<UpdateViewModel>();
}
window.Show();
}
这(不起作用):
private async Task InternalNavigateToAsync(Type viewModelType, object parameter)
{
var bootStrapper = new BootStrapper();
var container = bootStrapper.BootStrap();
Window window = CreateWindow(viewModelType, parameter);
//but how to do this?
window.DataContext = container.Resolve<viewModelType>();
window.Show();
}
它给了我一个错误:
'viewModelType' 是一个变量,但像类型一样使用
潇湘沐
相关分类