【问题标题】:How to use MvvmLight along with System.Windows.Interactivity?如何将 MvvmLight 与 System.Windows.Interactivity 一起使用?
【发布时间】:2016-05-26 15:17:39
【问题描述】:

非常简单的问题:如何将 MvvmLight 与 System.Windows.Interactivity 一起使用?!

这就是我的包裹的样子:

<packages>
  <package id="CommonServiceLocator" version="1.3" targetFramework="net452" />
  <package id="Expression.Blend.Sdk" version="1.0.2" targetFramework="net452" />
  <package id="MvvmLightLibs" version="5.2.0.0" targetFramework="net452" />
  <package id="Ninject" version="3.2.2.0" targetFramework="net452" />
</packages>

谁能建议我,我应该使用哪些版本来使这种代码正常工作,而不会出现错误/警告?

    <userControls:MyUserControl x:Name="Foo" Margin="10,110,10,10" BorderStrokeThickness="5">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="FooClick">
                <command:EventToCommand Command="{Binding Mode=OneWay, Path=ClickCommand}" 
                                        PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </userControls:MyUserControl>

我在EventToCommand 上收到不同的错误消息。现在我有错误:

无法将“EventToCommand”类型的实例添加到“TriggerActionCollection”类型的集合中。只允许使用“T”类型的项目。

几秒钟前我有:

程序集“GalaSoft.MvvmLight.Platform”中的“EventToCommand”类型是使用旧版本的 Blend SDK,在 Windows Presentation Framework 4 中不受支持 项目。

我什么也没做:/(重建/重新打开解决方案几次)

编辑:我刚刚重新编译,现在又出现第二个错误

EDIT2:

现在我有:

xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

我无法添加(我确实在引用中有 GalaSoft.MvvmLight.Extras dll)

xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"

【问题讨论】:

    标签: c# wpf xaml mvvm mvvm-light


    【解决方案1】:

    检查您的命名空间,它们应该是:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
    

    【讨论】:

    • 这太疯狂了。我已经安装了 MvvmLightLibs 5.2.0。但智能感知在我的 xaml 文件中没有看到 GalaSoft.MvvmLight.Command。我确实看到了这些 dll 的引用:/ 我现在有 xmlns:command="http://www.galasoft.ch/mvvmlight"
    • 在我的沙盒项目中重现你的代码。一切顺利:我能够在没有警告的情况下使用 EventToCommand 并且绑定到命令效果很好。那是我的实际命名空间:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:command="http://www.galasoft.ch/mvvmlight" 你能删除 nuget 包(卸载它们),然后从项目中删除所有引用,然后再次安装所需的引用吗?
    • @lerhe61 谢谢,你能检查我的简单代码吗:github.com/robertwojnar/MvvmDemo1 这只是一个视图,如果你能检查你的机器上是否出现错误
    • 试试你的代码,一切都好。加载窗口后获取已加载消息。
    • 它也适用于我,但我在设计器中有此错误,无法看到我的视图预览:/
    【解决方案2】:

    我刚刚偶然发现了这个解决方案(解决方法),它为我们提供了一种继续使用 XAML 工作并适当更新预览的方法。

    启用“启用项目代码”选项后,我会看到确切的错误。禁用此功能后,错误不再出现,可以继续进行生产性工作。

    这是困扰我(我相信还有很多其他人)数月的一根刺。希望导致此问题的潜在错误很快得到解决。在那之前,我祈祷这个答案对你和我一样有帮助。

    【讨论】:

      【解决方案3】:

      对于那些在这个页面上磕磕绊绊的人,我遇到了完全相同的问题,一切都在构建中工作,但设计师没有工作。

      我通过将目标框架从 4.6 设置为 .net framework 4

      解决了这个问题

      我就是这样做的

      1. 删除所有 galasoft nugget 引用并删除 system.windows.interactivity

      2. 转到项目设置 -> 应用程序-> 目标框架并在 .net Framework 4 上进行设置

      3. 放回引用,这次它们都将使用相同的框架

      这可能会导致由 system.Windows.interactivity 引起的构建错误 (文件未找到错误)

      1. 打开 App.config
      2. 移除“system.Windows.interactivity”依赖的Assembly

      【讨论】:

        【解决方案4】:

        通过执行以下操作,我能够通过 Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed

        1. 清洁溶液
        2. 删除binobj目录
        3. 重启 Visual Studio
        4. 重建解决方案

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-02
          • 2017-11-10
          • 1970-01-01
          • 2023-03-13
          • 2014-09-25
          • 2016-01-30
          • 2015-12-13
          • 2020-09-15
          相关资源
          最近更新 更多