“MainWindow”的部分声明不得使用通用基类

时间:2015-11-04 22:31:24

标签: c# wpf xaml

在我的主窗口中,我目前有

public partial class MainWindow : WpfView<MainWindowViewModel>
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

在我的XAML文件中,我有

<Window x:Class="wpfMvvm.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:wpfMvvm"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>
</Window>

我知道<Window></Window>需要更改为WpfView或其中的某种形式,但我还没有在SO或google上找到一个这样的例子。

有人知道XAML文件中的基本标记是什么吗?

2 个答案:

答案 0 :(得分:0)

我实际上在谷歌上找到了答案:

<local:WpfView x:Class="wpfMvvm.MainWindow"
           x:TypeArguments="local:MainWindowViewModel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:wpfMvvm"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

</Grid>
</local:WpfView>

使用local:WpfView作为基本标记,然后添加x:TypeArguments="local:MainWindowViewModel"属性。完全按照要求工作。

答案 1 :(得分:-1)

您在Xaml中指定的基类是Window

cs文件中指定的基类是WpfView<MainWindowViewModel>

基类类型需要匹配。

您可以通过创建非通用基类并在两个文件中使用它来解决这个问题:

   public class NonGenericWindow :  WpfView<MainWindowViewModel>
   {
   }