首页 新闻 会员 周边

MVVM模式下使用Prism的 DelegateCommand<T>为何会错误提示“T不是对象或者为空”?

0
[待解决问题]

Xaml里如下:

<ComboBox x:Name="comboType" Grid.Column="1" Grid.Row="4" HorizontalAlignment="Left" VerticalAlignment="Center" Width="150" Text="{Binding Question.Type}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SetTypeCommand}" CommandParameter="{Binding SelectedIndex,ElementName=comboType}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ComboBoxItem Content="设置题型..."></ComboBoxItem>
<ComboBoxItem Content="单选题"></ComboBoxItem>
<ComboBoxItem Content="多选题"></ComboBoxItem>

</ComboBox>

 

viewModel里如下:

private DelegateCommand<int> _setTypeCommand; 
public DelegateCommand<int> SetTypeCommand
{
get { return _setTypeCommand??(_setTypeCommand = new DelegateCommand<int>(SetType)); }
set { _setTypeCommand = value; }
}

private void SetType(int selectedIndex)
{
//
}

 

运行时就提示错误:T for DelegateCommand<T> is not an object nor Nullable。

试了下,如果这个T为string的话就不会出错,但为 int的话就报错,请问为什么?该怎么解决?谢谢!

WPF
仁兄的主页 仁兄 | 菜鸟二级 | 园豆:202
提问于:2017-12-08 16:51
< >
分享
所有回答(4)
1

改用 

int?
OpportunityLiu | 园豆:255 (菜鸟二级) | 2017-12-09 01:34
0

引用类型和值类型的区别

Lvwl-CN | 园豆:230 (菜鸟二级) | 2020-12-18 09:42
0

我也遇到这个问题了,你解决了吗?

Lucus_Taylor | 园豆:402 (菜鸟二级) | 2022-05-02 22:12
0

定义用int?
private DelegateCommand<int?> _setTypeCommand;

jumahe | 园豆:202 (菜鸟二级) | 2023-08-29 16:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册