【问题标题】:RelayCommand with Argument throwing MethodAccessException带有参数的 RelayCommand 抛出 MethodAccessException
【发布时间】:2013-10-28 09:14:06
【问题描述】:

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

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

“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 设为静态,它可以工作,但是我无法访问我的视图模型中的任何其他函数。

我一直试图弄清楚这一点,但没有任何运气。

【问题讨论】:

  • 认为这可能与我的 ViewModelLocator 类有关,该类目前使用以下内容:return ServiceLocator.Current.GetInstance();
  • 这被标记为已回答,但是:我将您的代码复制到我正在运行的现有项目中,然后单击 Test 超链接完美运行,没有烦躁或任何事情......您的定位器未定义,或者您的数据上下文不正确,或者您没有正确的 MVVM-Light 文件。如果您想尝试解决它,请留言,我会看看是否可以提供帮助。 (或者,您也可以使用自己的实现 :)

标签: .net mvvm arguments mvvm-light relaycommand


【解决方案1】:

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

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

【讨论】:

  • 谢谢 - 太好了。我实际上并没有实现我自己的relaycommand 类,我只是使用GalaSoft.MvvmLight.Command.RelayCommand 中的那个。我将尝试使用您在上面创建的那个,希望这就是我的问题。
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
相关资源
最近更新 更多