在使用 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' 是一个变量,但像类型一样使用
1 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
将类型作为参数传递给Resolve(Type serviceType)
window.DataContext = container.Resolve(viewModelType);
而不是试图将其用作通用参数
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消