基于Prism的CommandParameter参数类型为int时无响应

  用MVVM设计模式时可以选择Prism,肯定要用到命令绑定。关于命令传参的文章多如牛毛,陈老师的文章写得很精彩,建议大家去看看。网址如下:http://www.cnblogs.com/chenxizhang/archive/2012/04/14/2446782.html

  我在看完这个视频之后,立马就动手实践了,发现,当参数为string类型的时候,没有任何问题,当参数为int的时候就有问题了,其中的匿名方法体根本没有被执行。几经周折,终于找到了问题根源。

  新建一个wpf项目,加个VM:MainWindowViewModel.cs,用NuGet引入Prism包,就可以开始我们的demo了。

  MainWindow.xaml代码如下:

<Window x:Class="WpfDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cmd="clr-namespace:Microsoft.Practices.Prism.Commands;assembly=Microsoft.Practices.Prism"
        xmlns:local="clr-namespace:WpfDemo"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel ></local:MainWindowViewModel>
    </Window.DataContext>
    <Grid>
        <Button cmd:Click.Command="{Binding TemperatureUpCommand}"
                cmd:Click.CommandParameter="{Binding Temperature}"
            Content="Button" Height="23" HorizontalAlignment="Left" Margin="201,103,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>

  MainWindowViewModel.cs代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using Microsoft.Practices.Prism.ViewModel;
 6 using System.Windows.Input;
 7 using Microsoft.Practices.Prism.Commands;
 8 
 9 namespace WpfDemo
10 {
11     public class MainWindowViewModel:NotificationObject
12     {
13         private int _Temperature;
14 
15         public int Temperature
16         {
17             get { return _Temperature; }
18             set
19             {
20                 _Temperature = value;
21                 RaisePropertyChanged("Temperature");
22             }
23         }
24 
25         public ICommand TemperatureUpCommand
26         {
27             get
28             {
29                 return new DelegateCommand<int>(
30                     (Temp) =>
31                     {
32                         Temperature += 1;
33                     }
34                 );
35             }
36         }
37         
38     }
39 }

  至此环境就准备好了,然后跑起来,在31行打个断点,点击按钮,发现程序没有任何反应,断点也进不去。按照道理来讲是应该能进去的,大家如果都是string类型的肯定可以进去,前台的CommandParameter绑定string类型的属性,VM的DelegateCommand后面也改成string,此时就能进去。那么,大家都是int,也应该可以进去,为什么实际上却进不去呢?

  当你把后台的DelegateCommand的参数类型改成object时就能进去了,这也是一个解决办法,但是不是问题的根本,string的可以,为何int的就需要如此麻烦呢?

  接下来,在VS2010里依次打开调试→异常,弹出如下窗口:

  之前没有任何反应,也不报异常,是因为这里的勾勾没勾上,现在勾上再试试,果然,刚跑起来就报错了:

  现在明白了,原来问题在这里,把29行 return new DelegateCommand<int>( 改成 return new DelegateCommand<int?>( 问题就解决了。

  源代码下载:http://files.cnblogs.com/zoexia/WpfDemo.rar

转载于:https://www.cnblogs.com/zoexia/archive/2013/06/04/3118026.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于Prism的WPF MVVM示例,其中包含模型: 首先,创建一个名为“Models”的文件夹,并在其中添加一个名为“Person.cs”的类: ```csharp using System; namespace PrismExample.Models { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string Email { get; set; } } } ``` 接下来,创建一个名为“ViewModels”的文件夹,并添加一个名为“MainViewModel.cs”的类。该类将作为我们的主界面的视图模型: ```csharp using Prism.Commands; using Prism.Mvvm; using PrismExample.Models; namespace PrismExample.ViewModels { public class MainViewModel : BindableBase { private Person _person; public Person Person { get { return _person; } set { SetProperty(ref _person, value); } } public DelegateCommand SaveCommand { get; private set; } public MainViewModel() { Person = new Person(); SaveCommand = new DelegateCommand(Save); } private void Save() { // 保存Person信息的代码 } } } ``` 最后,创建一个名为“Views”的文件夹,并在其中添加一个名为“MainWindow.xaml”的文件。在该文件中,我们将使用数据绑定将视图模型绑定到UI元素: ```xaml <Window x:Class="PrismExample.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:PrismExample.ViewModels" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <vm:MainViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="First Name:" /> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Person.FirstName, UpdateSourceTrigger=PropertyChanged}" /> <Label Grid.Row="1" Grid.Column="0" Content="Last Name:" /> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Person.LastName, UpdateSourceTrigger=PropertyChanged}" /> <Label Grid.Row="2" Grid.Column="0" Content="Age:" /> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Person.Age, UpdateSourceTrigger=PropertyChanged}" /> <Label Grid.Row="3" Grid.Column="0" Content="Email:" /> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Person.Email, UpdateSourceTrigger=PropertyChanged}" /> <Button Grid.Row="4" Grid.Column="1" Content="Save" Command="{Binding SaveCommand}" /> </Grid> </Window> ``` 现在,运行应用程序,并尝试在文本框中输入一些信息,然后单击“保存”按钮,即可成功保存Person信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值