RelayCommand with Argument throwing MethodAccessException
Asked Answered
B

1

3

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

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:

A first chance exception of type 'System.MethodAccessException' occurred in mscorlib.dll

My code is below.

XAML

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

ViewModel

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";
}

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.

Bistro answered 19/10, 2013 at 16:5 Comment(2)
think this may have something to do with my ViewModelLocator class, which at the moment uses the below: return ServiceLocator.Current.GetInstance<QueryFormViewModel>();Bistro
This is marked as answered, but yet: I copied your code into an existing project I have running, and clicking on the Test hyperlink worked perfectly with no fidgeting or anything ... Either your locator wasn't defined, or your data context wasn't right, or you didn't have the right MVVM-Light files. If you want to try and solve it, leave a message and I'll see if I can help. (or, you can just use your own implementation as well :)Magalymagan
B
0

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

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!!");
    }
}

Output: TesterWorked!!

Bambara answered 28/10, 2013 at 14:36 Comment(1)
Thanks - thats great. I haven't actually implemented my own relaycommand class, I just use the one in GalaSoft.MvvmLight.Command.RelayCommand. I will try using the one you have created above, hopefully thats what my issue is.Bistro

© 2022 - 2024 — McMap. All rights reserved.