带参数的 RelayCommand 抛出 MethodAccessException [英] RelayCommand with Argument throwing MethodAccessException

查看:21
本文介绍了带参数的 RelayCommand 抛出 MethodAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .Net 和 MVVM Light 创建一个应用程序,但在使用 RelayCommands 时遇到了一些问题.

I am creating an application using .Net and MVVM Light and I am having some trouble for RelayCommands.

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

I'm trying to create a RelayCommand which takes in a single argument and passes it to a function within the same ViewModel. However everytime I try and do this I keep getting the following exception:

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

A first chance exception of type 'System.MethodAccessException' occurred in 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 设为静态,它可以工作,但是我无法访问视图模型中的任何其他函数.

If I make the function _testExecute static it works however I am unable to access any of the other functions within my viewmodel.

我已经尝试解决这个问题有一段时间了,但没有任何运气.

I have been trying to figure this out for a while now but not had any luck.

推荐答案

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

I don't know what your RelayCommand Class looks like, But i got yours to work using these class architectures.

RelayCommand 类:

#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!!

Output: TesterWorked!!

这篇关于带参数的 RelayCommand 抛出 MethodAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆