james33
james33

Reputation: 109

Generic Argument Constraint Violation?

I have declared a base dialog window class that types the datacontext to ensure that the attached viewmodel has the appropriate return type. When I try to use it though I get a generic arguments error:

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'.

I can't see why this would be happening

The base dialog window declaration:

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; }

}

The viewModel:

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

and the 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>

Code for the dialog:

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

The error that I referenced above is the only compiler error that I am getting. I would appreciate any insight into this error.

Update: I have now noticed that the application is compiling and running without any exceptions being thrown but the error is still there and the xaml designer is showing it as invalid markup

Upvotes: 2

Views: 294

Answers (1)

BionicCode
BionicCode

Reputation: 29028

IsDesignTimeCreatable=False will force the designer to ignore the specified DesignInstance type and create a substitute type using reflection. In this case the designer failed to recognize the generic type as it is a complex type rather than a primitive type and therefore failed to create a proper mock instance with a proper generic parameter TViewModel.

To solve this, you could set the IsDesignTimeCreatable property to True and implement a default constructor on ReceiveItemViewModel. If a default constructor is not possible, introduce a wrapper type just for the design time DesignInstance and spend it a a default constructor that initializes the base type ReceiveItemViewModel properly.

Upvotes: 1

Related Questions