RelayCommand with Argument抛出MethodAccessException

时间:2013-10-19 16:05:28

标签: .net mvvm arguments mvvm-light relaycommand

我正在使用.Net和MVVM Light创建一个应用程序,我在RelayCommands上遇到了一些麻烦。

我正在尝试创建一个RelayCommand,它接受一个参数并将其传递给同一ViewModel中的一个函数。但是,每次我尝试这样做时,我都会遇到以下异常:

  

类型'System.MethodAccessException'的第一次机会异常   发生在mscorlib.dll

我的代码如下。

XAML

<TextBlock Style="{StaticResource QueryFormTab}" >
    <Hyperlink Command="{Binding TestCommand}" CommandParameter="Tester">
        Test
    </Hyperlink>
</TextBlock>

视图模型

public RelayCommand<string> TestCommand { get; private set; }

// in the constructor 
TestCommand = new RelayCommand<string>((param) => _testExecute(param)); 

// function in viewmodel
private void _testExecute(string s)
{
    Trace.WriteLine("Test");
    ViewModelVariable = "abc";
}

如果我将函数_testExecute设为静态它可以工作但是我无法访问我的viewmodel中的任何其他函数。

我一直试图想出这个问题一段时间但没有运气。

1 个答案:

答案 0 :(得分:0)

我不知道你的RelayCommand Class是什么样的,但我让你的工作使用这些类架构。

RelayCommand Class:

#region Referenceing

using System;
using System.Diagnostics;
using System.Windows.Input;

#endregion

public class RelayCommand : ICommand
{
    #region Fields 

    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

    #endregion // Fields 

    #region Constructors 

    public RelayCommand(Action<object> execute) : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    #endregion // Constructors 

    #region ICommand Members 

    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null || _canExecute(parameter);
    }

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

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

    #endregion // ICommand Members 
}

<强> XAML:

<Window x:Class="StackTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:this="clr-namespace:StackTest"
        Title="MainWindow" Height="350" Width="525">
  <Window.DataContext>
    <this:ViewModel/>
  </Window.DataContext>
    <Grid>
    <TextBlock>
    <Hyperlink Command="{Binding TestCommand}" CommandParameter="Tester">
        Test
    </Hyperlink>
    </TextBlock>
  </Grid>
</Window>

<强> XAML.cs:

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

<强> ViewModel.cs:

public class ViewModel
{
    private ICommand _testCommand;

    public ICommand TestCommand
    {
        get { return _testCommand ?? (_testCommand = new RelayCommand(_testExecute)); }
    }

    private void _testExecute(object s)
    {
        Trace.WriteLine(s + "Worked!!");
    }
}

输出: TesterWorked !!