如何向xaml添加多个泛型类型参数

问题描述 投票:1回答:1

我的Xaml页面具有此基类,其定义如下:`

public abstract class BasePageView<T, VM> : Page where T : class, IBaseClass, new() where VM : BaseMainViewModel<T>,new()

我想将其用作对新页面的引用,并且我想填充自变量类型,如下所示:`

<Commons:BasePageView x:TypeArguments="Db:Project, Commons:BaseMainViewModel" x:Class="Facade_User.Pages.Page_ProjectConfig"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:Facade_User.Pages"
  xmlns:Db="clr-namespace:Facade_DB;assembly=Facade_DB"
  xmlns:Commons="clr-namespace:WpfCommons;assembly=WpfCommons"

我看到的错误是:

Error XDG0062
GenericArguments[1], 'System.Object', on 'WpfCommons.BasePageView2[T,VM]' violates
 the constraint of type 'VM'.Facade_User
Page_ProjectConfig.xaml 1

我真的可以使用帮助。

c# wpf xaml generics
1个回答
0
投票

我已经找到了解决方案,这是我的粗心大意。我要求使用基本ViewModel,例如当我想调用ViewModel而不是真正的ViewModel时,显示为Commons: BaseMainViewModel。我解决了这个问题:

<Commons:BasePageView x:TypeArguments="local:ProjectViewModel, Db:Project"
© www.soinside.com 2019 - 2024. All rights reserved.