使用: WPF,Prism 5,Unity
我正在尝试编写我称之为" WindowDialogService"当传递一个类型并被调用时,它将打开一个窗口,该类型作为内容。我的问题是我无法让ViewModel实例化并关联到View。
我不知道它是否正确,但我在我的视图中使用import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
Select your favorite serie:
<select (change)="onChange($event.target.value)">
<option *ngFor="let serie of series" [value]="serie.id">{{ serie.name }}</option>
</select>
<br />
<br />
Selected Serie: <br />
<span *ngIf="selectedSerie">
{{ selectedSerie.id }} - {{ selectedSerie.name }}
</span>
`
})
export class AppComponent {
private series: any[];
private selectedSerie: any;
constructor() {
this.series = [
{ id: 1, name: 'Friends' },
{ id: 2, name: 'How I met Your Mother' },
{ id: 3, name: 'Modenr Family' }
];
this.selectedSerie = this.series[0];
}
onChange(serieId: any): void {
this.selectedSerie = this.series.find(serie => serie.id == serieId);
}
}
,并假设(显然不正确)ViewModel将位于&#34;位于&#34;使用Unity容器进行解析时,即AutoWireViewModel
基本上,我可以解析View,但视图的DataContext为null。
我已在下面提供了所有相关代码。
查看
container.TryResolve <T> ()
视图模型
<dxc:DXWindow x:Class="ListClassList.Views.AddToClassView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:dxe="clr-namespace:DevExpress.Xpf.Editors.Settings;assembly=DevExpress.Xpf.Core.v15.2"
xmlns:dxeditors="http://schemas.devexpress.com/winfx/2008/xaml/editors"
xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
WindowStartupLocation="CenterScreen"
dxc:ThemeManager.ThemeName="VS2010"
Title="{Binding Title}"
Width="800"
Height="500"
ResizeMode="CanResizeWithGrip"
>
<Grid>
</Grid>
</dxc:DXWindow>
IWindowDialogService接口
using MyApp.Data;
using MyApp.Models.Model;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace ListClassList.ViewModels
{
public class AddToClassViewViewModel : BindableBase
{
private readonly MyAppDbContext _context;
private ObservableCollection<MasterClass> _masterClasses;
public ObservableCollection<MasterClass> MasterClasses
{
get { return _masterClasses; }
set { SetProperty(ref _masterClasses, value); }
}
private ObservableCollection<Student> _students;
public ObservableCollection<Student> Students
{
get { return _students; }
set
{
SetProperty(ref _students, value);
}
}
public DelegateCommand FinishCommand { get; set; }
public AddToClassViewViewModel(IStudentTimetableService studentTimetableService)
{
}
}
}
WindowDialogService实施
using System;
using System.Windows;
namespace MyApp.Infrastructure.Services.Dialogs.WindowDialog
{
public interface IWindowDialogService
{
bool? ShowDialog<T>(Action onDialogClosed) where T : Window;
}
}
答案 0 :(得分:2)
Prism 6会自动执行此操作,对于Prism 5,您需要一些符合
的内容ViewModelLocationProvider.SetDefaultViewModelFactory( type => Container.Resolve( type ) );
在你的引导程序ConfigureContainer