【问题标题】:"specified cast is not valid" GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(Object parameter)“指定的演员表无效” GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(对象参数)
【发布时间】:2011-06-23 16:38:48
【问题描述】:

我对使用 MVVM Light 还是很陌生,所以希望这是一个简单的解决方法,尽管我花了一天的大部分时间来寻找答案:-(

在我的 xaml 中

<sdk:DataGrid Name="m_dgResults" AutoGenerateColumns="False" IsReadOnly="True" AreRowDetailsFrozen="True" SelectionMode="Single" ItemsSource="{Binding SearchResults}" >

.
.
.

<Button x:Name="cmdFTSViewText" Width="24" Height="24" Margin="5"  ToolTipService.ToolTip="View Text">
  <Image Source="../Images/ViewDocumentText.png" Stretch="None"/>
  <i:Interaction.Triggers>
      <i:EventTrigger EventName="Click">
          <cmd:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=FindModel.ViewDocumentTextCommand}" 
                              CommandParameter="{Binding iUniqueID}"/>
      </i:EventTrigger>
  </i:Interaction.Triggers>
 </Button>

在我的视图模型中

public RelayCommand<int> ViewDocumentTextCommand { get; private set; }
public FindModel() 
{
    .
    .
    .
    ViewDocumentTextCommand = new RelayCommand<Int32>(p => { ViewDocumentText(p); });
}

public void ViewDocumentText(Int32 iDocumentID)
{
    .
    .
    .
}

SearchResults.iUniqueID 是一个 Int32

由于某种原因,按下按钮时会引发上述异常。

谢谢

【问题讨论】:

标签: c# exception mvvm mvvm-light relaycommand


【解决方案1】:

可能是因为您的 RelayCommand 被定义为 RelayCommand&lt;int&gt;,而您正试图为其分配 RelayCommand&lt;Int32&gt;

尝试将您的命令定义改为简单的ICommand

也可能是在初始绑定之前该值为 null。尝试使用对象而不是指定类型。

new RelayCommand(p =&gt; ViewDocumentText(p));

public void ViewDocumentText(object iDocumentID)

【讨论】:

  • 感谢您的指点。 / 不是问题。那只是我报告问题时的错误。我一直在尝试两种变体来诊断问题并忘记在发布前再按一次撤消:$ 替换
  • 代入 ViewDocumentTextCommand = new RelayCommand(p => { ViewDocumentText(p); });让代码工作并允许我诊断问题:-) 结果我的 Int32 最终在 xaml 中作为一个字符串。我现在认为这与 Silverlight.DataSet.DataView 对象的 GetBindableData 成员有关。再次感谢。
  • XAML 中的字符串不会自动正确转换,这实际上很难正确转换。我计划在 MVVM Light 的未来版本中尝试让它变得更好,但这并不容易。干杯!
  • 我遇到了这个问题,一个视图模型工作,另一个抛出了“指定的演员表无效”异常......我追踪到这个“工作视图模型是”使用 GalaSoft.MvvmLight.CommandWpf; ” 不起作用的是“使用 GalaSoft.MvvmLight.Command;”(缺少“Wpf”)....
猜你喜欢
  • 2014-07-09
  • 2018-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多