WPF&MVVM:Library System.Windows.Interactivity 不再可用?

4

我需要通过引用管理器添加System.Windows.Interactivity.dll库。在Visual Studio 2017中,我没有找到它。以下是从System.Windows开始的所有搜索结果,如下图所示:

enter image description here

我以为这个库已经成为当前 Visual Studio 版本中 WPF 项目的内置部分,所以当我添加下面的 C# 代码时,没有显示任何来自 IDE 的警告:
private RelayCommand doubleCommand;
public RelayCommand DoubleCommand {
    get {
        return doubleCommand ??
        (doubleCommand = new RelayCommand(obj => {
            Phone phone = obj as Phone;
            if (phone != null) {
                Phone phoneCopy = new Phone {
                    Company = phone.Company,
                    Price = phone.Price,
                    Title = phone.Title
                };
                Phones.Insert(0, phoneCopy);
            }
        }));
    }
}

然而,当我添加以下的 XAML 标记时,它会提示在 clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity 命名空间中找不到 InteractionEventTriggerInvokeCommandAction
<Window x:Class="MVVM_Tutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVM_Tutorial"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

<!-- ... -->

    <Button Content="2x">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <i:InvokeCommandAction
                    Command="{Binding DoubleCommand}"
                    CommandParameter="{Binding SelectedPhone}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

<!-- ... -->

</Window>

这是否与 System.Windows.Interactivity 库有关,我该怎么解决这个问题?

3个回答

5
目前,需要通过 NuGet 包管理器添加 System.Windows.Interactivity.WPF。请参考以下截图操作:enter image description here

3

我发现现在有一个名为Microsoft.XAML.Behaviors的NuGet包,之前因为在Visual Studio 2022中错误地引用WPF Interactivity而着陆到这个问题。希望能帮助到其他人。

微软将WPF Interactivity开源,此前它以各种方式作为WPF的一部分提供。请参阅博客文章:link

NuGet包: Microsoft.XAML.Behaviors

XAML引用: xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

绑定TextBox失去焦点时的命令的示例XAML代码:

<TextBox Name="txtFilterText" Text="{Binding SmartFilterText}" KeyUp="txtFilterText_KeyUp">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:InvokeCommandAction 
                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TextBox}, Path=DataContext.FilterSongsCommand}" 
                CommandParameter="{Binding}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接