0
使用: WPF,棱鏡5,統一棱鏡5和Unity - 手動解決視圖與視圖模型連接好
我想寫我稱之爲「WindowDialogService」,當傳遞的類型,並呼籲,將以該類型作爲內容打開一個窗口。我的問題是我無法讓ViewModel實例化並關聯到視圖。
我不知道這是否是正確的,但我用我的視圖AutoWireViewModel
,並假設(顯然不正確地),該視圖模型將被「位於」使用Unity容器即container.TryResolve <T>()
所以基本上解決的時候,我可以解析視圖,但視圖的DataContext爲null。
我在下面列出了所有相關的代碼。
查看
<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>
視圖模型
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)
{
}
}
}
IWindowDialogService接口
using System;
using System.Windows;
namespace MyApp.Infrastructure.Services.Dialogs.WindowDialog
{
public interface IWindowDialogService
{
bool? ShowDialog<T>(Action onDialogClosed) where T : Window;
}
}
WindowDialogService實施
using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Practices.ServiceLocation;
using Prism.Unity;
namespace MyApp.Infrastructure.Services.Dialogs.WindowDialog
{
public sealed class WindowDialogService : IWindowDialogService
{
private readonly List<Window> _openWindows = new List<Window>();
private static volatile WindowDialogService _instance;
private static readonly object SyncRoot = new Object();
private WindowDialogService() { }
public static WindowDialogService Instance
{
get
{
if (_instance == null)
{
lock (SyncRoot)
{
if (_instance == null)
_instance = new WindowDialogService();
}
}
return _instance;
}
}
public bool? ShowDialog<T>(Action onDialogClosed) where T : Window
{
try
{
using (var container = new Microsoft.Practices.Unity.UnityContainer())
{
var dialog = (Window)container.TryResolve <T>();
dialog.Closed += (s, e) => onDialogClosed();
var result = dialog.ShowDialog();
return result;
}
}
catch (Exception)
{
throw;
}
}
}
}
感謝Haukinger東西。我試過這個,但似乎沒有解決這個問題。有什麼我需要改變我的代碼?我將這行代碼(完全如圖所示)放入我的引導程序中的ConfigureContainer中。我解決視圖的方式(創建UnityContainer並使用TryResolve)是否正確? –
我會使用引導程序中的一個統一容器(將它作爲服務的依賴項注入),以防您的窗口具有自己的任何依賴關係。當然,視圖模型的所有依賴關係都必須註冊到用於解析視圖模型的容器中。 – Haukinger
好吧,我的壞! *啞*我最近升級到棱鏡6導致一個突破的變化,視圖不能結束在「視圖」(和ViewModels不能結束在「ViewViewModel」。見這裏:github.com/PrismLibrary/Prism。將標記你的作爲正確的答案,因爲它確實解決了問題;-) –