使用通用parmater创建ViewModel

时间:2014-03-27 03:19:44

标签: c# wpf generics mvvm

我正在使用MVVM模式,我正在尝试创建通用对话框以使用xml文件导入数据。

所以,我需要的是将T对象传递给数据服务以获取视图设置并从XML文件执行转换逻辑并返回到我在视图设置中的对象列表。

例如,我创建数据服务如下所示:

public class CategoryHierarchyFileImportDataService: ICategoryHierarchyFileImportDataService
    {
    #region Public Methods

    public virtual CategoryHierarchyFileImportViewSettings<T> GetCategoryHierarchyFileImportViewSettings<T>()
    {
        var result = _mappingEngine.Map<List<ApplicationObjectDTO>, CategoryHierarchyFileImportViewSettings<T>>(_viewApplicationObjects[ViewNames.CategoryHierarchyFileImportView]);
        result.DocumentProperties.Caption = result.ListCaptionLabelContent;

        return result;
    }}

所以我想创建这样的视图模型:

    public class CategoryHierarchyFileImportViewModel<T> : BindableBase, IPopUpViewModel
{
    #region Private Variables

    private readonly ICategoryHierarchyFileImportDataService _categoryHierarchyFileImportDataService;
    private readonly IOpenFileDialogWrapperService _openFileDialogWrapper;

    #endregion

    #region Properties

    public DelegateCommand BrowseFilesCommand { get; set; }
    public DelegateCommand ImportCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }
    public CategoryHierarchyFileImportViewSettings<T> ViewSettings { get; set; }

    #endregion

    #region Constructors


    public CategoryHierarchyFileImportViewModel(
        ICategoryHierarchyFileImportDataService categoryHierarchyFileImportDataService,
        IApplicationObjectDataService applicationObjectDataService,
        IOpenFileDialogWrapperService openFileDialogWrapper)
    {
        _categoryHierarchyFileImportDataService = categoryHierarchyFileImportDataService;
        _openFileDialogWrapper = openFileDialogWrapper;
        applicationObjectDataService.LoadApplicationObjects(ViewNames.CategoryHierarchyFileImportView);
        ImportCommand = new DelegateCommand(ExecuteImportCommand);
        CancelCommand = new DelegateCommand(ExecuteCancelCommand);
        BrowseFilesCommand = new DelegateCommand(ExecuteBrowseFilesCommand);
        ViewSettings = _categoryHierarchyFileImportDataService.GetCategoryHierarchyFileImportViewSettings<T>();
    }
}

但问题是我无法创建像这样的视图模型,因为View会抱怨,我将无法绑定视图模型。

我可以解决这个问题吗?

0 个答案:

没有答案