【问题标题】:MVVM Light, Mahapps metro and EventToCommand via EventTrigger from System.Windows.Interactivity通过 System.Windows.Interactivity 中的 EventTrigger 实现 MVVM Light、Mahapps Metro 和 EventToCommand
【发布时间】:2020-07-28 20:18:03
【问题描述】:

我要疯了: 我有一个同时使用 Mahapps.Metro 和 MVVMLight 的应用程序。 基本上大多数事情都可以,直到您尝试使用 Interaction.Triggers。

我通常会遇到类似的错误

Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.

通过我在网上找到的 repo 可以重现此问题: https://github.com/mike-schulze/mahapps-mvvmlight-setup (抱歉,mike 劫持了)并向视图模型添加 Metro SplitButton:

<Controls:SplitButton ItemsSource="{Binding MyList}" Grid.Column="1" Grid.Row="1">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <cmd:EventToCommand 
                                    Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" 
                                    PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Controls:SplitButton>

MyList 是

        public List<string> MyList
        {
            get
            {
                List<string> theList = new List<string>();
                theList.Add("Hello");
                theList.Add("World");
                return theList;
            }
        }

我不确定 App.config 中有多少不同的命名空间、硬编码的交互性版本,或者我曾经尝试过什么。

有没有人使用 Mahapps Metro(版本不太旧)和 MVVM Light 以及 EventToCommand 的东西的工作示例?

我认为我的问题与 Metro 的更新有关 - 他们现在使用的是行为而不是交互性。我正在使用 Interactivity dll 版本 4.5.0.0。 但即使是我上面提到的旧 git 也显示了问题......而且我找不到有效的解决方案。

非常感谢任何帮助。 谢谢

【问题讨论】:

    标签: wpf mvvm-light mahapps.metro eventtocommand


    【解决方案1】:

    您还需要为 System.Windows.Interactivity 添加命名空间。

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

    因为 MVVMLight 使用了这个版本的 Interactivity。

    <mah:MetroWindow x:Class="..."
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
                     ...
    
                     xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
                     xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                     xmlns:command="http://www.galasoft.ch/mvvmlight"
    
                     ...
    
                     DataContext="{Binding Main, Source={StaticResource Locator}}">
    
        ...
        
        <mah:SplitButton ItemsSource="{Binding MyList}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <command:EventToCommand Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </mah:SplitButton>
    
        ...
    
    </mah:MetroWindow>
    

    【讨论】:

    • 嗨,我假设你是 'the' mahapps punker76 :) 很高兴认识你。在尝试解决问题时,我还使用了交互性命名空间——但没有取得多大成功。因此,遵循该方法会产生The type 'EventToCommand' from assembly 'GalaSoft.MvvmLight.Platform' is built with an older version of the Blend SDK, and is not supported in a Windows Presentation Framework 4 project。我在这里得到了什么stackoverflow.com/questions/8360209/… 我想我在行为与交互方面存在一些冲突。
    • @Vegetico 这两个库是具有不同命名空间的不同 dll,因此它可以一起工作。我回答的帖子来自一个工作示例。
    • 你能分享这个项目吗?我的另一个想法是它与 .NET 框架有关——基于上面的链接。我的 xaml 看起来和你的一模一样 - 所以我猜它介于 .NET 版本和所用组件的 lib 版本之间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多