读书人

100分!WPF 的有关问题 请们进来看看

发布时间: 2013-04-02 12:35:26 作者: rapoo

100分求助!!!WPF 的问题 请大虾们进来看看 比较急!!!
WPF程序采用了MVVM模式编程 ,在程序设计中我需要往ICommand传window窗体参数,可是当我绑定参数后,vs2010设计器就报错,不能显示正常的设计界面,如下图
100分!WPF 的有关问题 请们进来看看 比较急

把我代码贴出来,麻烦大虾们帮忙解决
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就不会有问题了,如图
100分!WPF 的有关问题 请们进来看看 比较急

求大侠指教!!!!!
[解决办法]
_canExecute((T)parameter);
里面 的参数转换失败,A类型不能转换到B类型
[解决办法]
wpf 是什么????
[解决办法]
错误应该出在 _canExecute((T)parameter

读书人网 >C#

热点推荐