0

我正在尝试构建用于使用 WinRT / Metro 进行 MVVM 的复合部件。我已经构建了自己的 ViewBase,它派生自 UserControl,因此我可以使用它来扩展我的视图:

using System;
using System.Linq;
using Windows.UI.Xaml.Controls;
using System.Collections.Generic;

public abstract class ViewBase : UserControl
{
    /// <summary>
    /// Initializes a new instance of the ViewBase class.
    /// </summary>
    public ViewBase() : base()
    {
        BindViewModelLocatorToView(viewModelLocator: GetViewModelLocator());
    }

    /// <summary>
    /// Defines a method that returns a view model locator to be used with this class.
    /// </summary>
    protected abstract IViewModelLocator GetViewModelLocator();


    /// <summary>
    /// Defines a method that Bind's the view model provided by the view locator to the view's data context.
    /// </summary>
    private void BindViewModelLocatorToView(IViewModelLocator viewModelLocator)
    {
        if (viewModelLocator != null)
        {
            DataContext = viewModelLocator.ViewModel;
        }
    }
}

当我尝试扩展我用作视图的 UserControl 时,我收到一个错误,即此部分的父级必须与生成的部分的父级匹配。当然,我不能更改生成的类父级,因为它只是在重建时更改回来。

我注意到在示例应用程序中,它们使用与布局感知页面相同的概念,但我不确定它们如何设置设计器生成的部分以匹配。

有谁知道这是怎么做到的?

4

1 回答 1

1

您需要将 XAML 中的根元素更改为<ns:ViewBase>,其中ns声明为您的类所在的命名空间。

于 2012-07-22T14:31:32.720 回答