2013-10-19 27 views
2

我正在使用.Net和MVVM Light創建應用程序,並且對RelayCommands有些麻煩。使用參數拋出的RelayCommand MethodAccessException

我正在嘗試創建一個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中訪問的任何其他功能。

我一直在試圖弄清楚這一點,但沒有任何運氣。

+0

認爲這可能與我的ViewModelLocator類有關,它目前使用下面的方法:return ServiceLocator.Current.GetInstance (); – user2898150

+0

這被標記爲已回答,但是:我將你的代碼複製到我已經運行的現有項目中,然後單擊「測試」超鏈接,完美地工作,沒有任何煩躁或任何事情......您的定位器未定義,或者您的定位器數據上下文不對,或者您沒有正確的MVVM-Light文件。如果你想嘗試解決它,請留言,我會看看我能否提供幫助。 (或者,你可以使用自己的實現以及:) – Noctis

回答

0

我不知道你的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 !!

+0

謝謝 - 多數民衆贊成在很大程度上。我實際上並沒有實現自己的relaycommand類,我只是使用GalaSoft.MvvmLight.Command.RelayCommand中的一個。我會嘗試使用上面創建的那個,希望那是我的問題。 – user2898150

+0

它是如何工作的? –

+1

是的,工作。謝謝Trae,那幫助我太多了! :) – user2898150

相關問題