c# - 违反通用参数约束?

标签 c# wpf xaml

我已经声明了一个基础对话框窗口类,它对数据上下文进行类型化,以确保附加的 View 模型具有适当的返回类型。当我尝试使用它时,虽然出现了通用参数错误:

GenericArguments[1], 'Mocks.MidSoft_Hospitality_ViewModels_Dialogs_ReceiveItemViewModel_32_569724456', on 'Mocks.MidSoft_Hospitality_Views_Dialogs_BaseDialogWindow`2_32_569724456[TResult,TViewModel]' violates the constraint of type 'TViewModel'.

我不明白为什么会这样

基本对话框窗口声明:

public class BaseDialogWindow<TResult, TViewModel> : DialogWindowBase<TResult> where TViewModel: ViewModels.Dialogs.DialogBaseViewModel<TResult>
{
    public BaseDialogWindow() : base()
    {

    }

    new public TViewModel DataContext
    {
        get => this.GetValue(DataContextProperty) as TViewModel;
        set => this.SetValue(DataContextProperty, value);
    }

}

DialogWindowBase:

public class DialogWindowBase<TResult> : Window, IDialog<TResult>
{
    public DialogWindowBase()
    {
        //Formatting code here
    }

    public Result Result { get; set; } = Result.None;

    public TResult ReturnData { get; set; }

}

View 模型:

public class ReceiveItemViewModel : ViewModels.Dialogs.DialogBaseViewModel<ReceiveItemResult>
{
    //View Model Code here
}

和 xaml:

<local:BaseDialogWindow x:Class="MidSoft.Hospitality.Views.Dialogs.ReceiveItemDialog"
                        x:TypeArguments="local:ReceiveItemResult, vm:ReceiveItemViewModel"
                        xmlns:vm="clr-namespace:MidSoft.Hospitality.ViewModels.Dialogs"
                        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:MidSoft.Hospitality.Views.Dialogs"
                        mc:Ignorable="d"
                        d:DataContext="{d:DesignInstance vm:ReceiveItemViewModel, IsDesignTimeCreatable=False}"
                        x:Name="ReceiveStockItemDialog"
                        Height="450" Width="800">
    <Grid>
    </Grid>
</local:BaseDialogWindow>

对话框代码:

    public partial class ReceiveItemDialog
    {
        public ReceiveItemDialog()
        {
            InitializeComponent();
        }
    }

我在上面提到的错误是我遇到的唯一编译器错误。如果能深入了解此错误,我将不胜感激。

更新:我现在注意到应用程序正在编译和运行,没有抛出任何异常,但错误仍然存​​在,xaml 设计器将其显示为无效标记

最佳答案

IsDesignTimeCreatable=False 将强制设计人员忽略指定的 DesignInstance 类型并使用反射创建替代类型。在这种情况下,设计者无法识别泛型类型,因为它是复杂类型而不是原始类型,因此无法创建具有适当泛型参数 TViewModel 的适当模拟实例。

要解决此问题,您可以将 IsDesignTimeCreatable 属性设置为 True 并在 ReceiveItemViewModel 上实现默认构造函数。如果无法使用默认构造函数,请仅为设计时引入包装器类型 DesignInstance 并将其用作默认构造函数,以正确初始化基类型 ReceiveItemViewModel

关于c# - 违反通用参数约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689856/

相关文章:

xaml - 如何在 UWP XAML NavigationView MenuItems 中混合动态和静态项?

c# - VS 2015 "if"条件执行到错误的 block

c# - UIControls 显示在父控件边界之外

c# - WPF 默认 DynamicRessource 值

c# - 我如何一次为一个堆栈面板项目制作动画?

.net - 没有 Windows Media Player 10+ 的 WPF 中的媒体支持?

c# - 如何将指针从 C# 传递到 DLL 中的 native 函数?

c# - 如何通过比较字符串出现的位置对字符串列表进行排序?

c# - Mono for android 使用授权的 Web 服务

wpf - ViewModel 中 IDisposable 内存泄漏,为什么?