100分求助!!!WPF 的问题 请大虾们进来看看 比较急!!!

沐秋 2013-03-29 12:48:07
WPF程序采用了MVVM模式编程 ,在程序设计中我需要往ICommand传window窗体参数,可是当我绑定参数后,vs2010设计器就报错,不能显示正常的设计界面,如下图


把我代码贴出来,麻烦大虾们帮忙解决
RelayCommand.cs

public class RelayCommand<T>:ICommand
{
//定义执行命令成员和是否可执行命令成员
readonly Action<T> _execute;
readonly Func<T,bool> _canExecute;

//构造函数
public RelayCommand(Action<T> execute)
: this(execute, null)
{

}

public RelayCommand(Action<T> execute, Func<T,bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("RelayCommand执行命令不可预知");
_execute = execute;
_canExecute = canExecute;
}

public bool CanExecute(T parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}

public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

public void Execute(T parameter)
{
_execute(parameter);
}

public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}

public void Execute(object parameter)
{
_execute((T)parameter);
}
}


ViewModelBase.cs

public abstract class ViewModelBase : INotifyPropertyChanged,IDisposable
{
protected ViewModelBase() { }

~ViewModelBase() { }

//属性更改事件
public event PropertyChangedEventHandler PropertyChanged;



protected virtual bool ThrowOnInvalidPropertyName
{
get;
private set;
}

public void Dispose()
{
OnDispose();
}
protected virtual void OnDispose()
{
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
//this.VerifyPropertyName(propertyName);
var hanlder = this.PropertyChanged;
if (hanlder != null)
{
hanlder(this, e);
}
}
/// <summary>
/// 提示属性已更改
/// </summary>
/// <param name="propertyName">更改的属性名称</param>
public void RaisePropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

/// <summary>
/// 提示属性已更改
/// </summary>
/// <typeparam name="TProperty">更改的属性类型</typeparam>
/// <param name="expr">更改的属性表达式</param>
public void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> expr)
{
MemberExpression memberExpr = (MemberExpression)expr.Body;
string memberName = memberExpr.Member.Name;
RaisePropertyChanged(memberName);
}

[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "无效的属性名称:"+propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
}


MainWindwoViewModel.cs

public class MainWindowViewModel:ViewModelBase
{
private ICommand _closeMainWindow;

public ICommand CloseMainWindow
{
get
{
if (_closeMainWindow == null)
{
_closeMainWindow = new RelayCommand<Window>(CloseMainWindowAction,CanCloseMainWindowAction);
}
return _closeMainWindow;
}

}

private void CloseMainWindowAction(Window parameter)
{
if (MessageBox.Show("确定关闭客户端主窗口吗?", "长今提示", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
parameter.Close();
}
}

private bool CanCloseMainWindowAction(Window parameter)
{
return true;
}
}


xaml绑定代码

<Button Content="退出" Height="19" Name="BtnExit" Width="40" FontSize="14" Margin="5,5,0,0" Style="{StaticResource LinkBtnStyle}"
Template="{StaticResource LinkBtnTemp}" Command="{Binding CloseMainWindow}" CommandParameter="{Binding ElementName=MainWindow}"/>


在RelayCommand代码里下图处出现的转换错误 如果直接返回True就不会有问题了,如图


求大侠指教!!!!!
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gxingmin 2013-03-29
  • 打赏
  • 举报
回复
_canExecute((T)parameter); 里面 的参数转换失败,A类型不能转换到B类型
沐秋 2013-03-29
  • 打赏
  • 举报
回复
顶一顶 路过的帮忙顶顶啦 感激不尽
沐秋 2013-03-29
  • 打赏
  • 举报
回复
我自己找到解决问题了 可能是绕了些弯路吧,大侠看到勿拍,上代码,散分


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Diagnostics;

namespace STLogisticsRelayCommand
{
    public class RelayCommand<T>:ICommand
    {
        //定义执行命令成员和是否可执行命令成员
        readonly Action<T> _execute;
        readonly Predicate<object> _canExecute;

        //构造函数
        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {

        }

        public RelayCommand(Action<T> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("RelayCommand执行命令不可预知");
            _execute = execute;
            _canExecute = canExecute;
        }

        public bool CanExecute(T parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(T parameter)
        {
            _execute(parameter);
        }

        public bool CanExecute(object parameter)
        {
           return _canExecute == null ? true : _canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
    }
}
沐秋 2013-03-29
  • 打赏
  • 举报
回复
引用 7 楼 tiana0 的回复:
错误应该出在 _canExecute((T)parameter
老兄 我当然知道是处在这里了 呵呵
三五月儿 2013-03-29
  • 打赏
  • 举报
回复
错误应该出在 _canExecute((T)parameter
gxingmin 2013-03-29
  • 打赏
  • 举报
回复
引用 4 楼 alvindosth 的回复:
引用 2 楼 gxingmin 的回复:_canExecute((T)parameter); 里面 的参数转换失败,A类型不能转换到B类型 这样写的目的就是 可以传window参数进去,但是在 C# code?12345 public void Execute(object parameter) { _execute((T)p……
那你不要三元运算符,改成if else分支试试
_小黑_ 2013-03-29
  • 打赏
  • 举报
回复
wpf 是什么????
沐秋 2013-03-29
  • 打赏
  • 举报
回复
引用 2 楼 gxingmin 的回复:
_canExecute((T)parameter); 里面 的参数转换失败,A类型不能转换到B类型
这样写的目的就是 可以传window参数进去,但是在

 public void Execute(object parameter)
        {
            _execute((T)parameter);
        }
 
这就不会报错,求大侠指点
沐秋 2013-03-29
  • 打赏
  • 举报
回复
这样写的目的就是 可以传window参数进去,但是在

 public void Execute(object parameter)
        {
            _execute((T)parameter);
        }

这就不会报错,求大侠指点

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧