XAML 将命令绑定到视图的Loaded事件

6qfn3psc  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(231)

我试图在视图完成加载时运行一个方法。我试图将一个命令绑定到视图中的Loaded事件,但它无法运行。引发的内部异常是
'在' System.Windows.Data.Binding '上提供值时掷回例外状况。'行号'14'且行位置'14'

<UserControl x:Class="Components.Map.MapView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:map="clr-namespace:Components.Map"
             xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
             xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
             xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
             xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
             xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
             xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
             xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
             xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             Loaded="{Binding Path=MapControlViewModel.MapLoadedCommand}">

如何绑定到视图的Loaded事件,以便在视图完成加载后运行某些操作?

dgjrabp2

dgjrabp21#

如果要将命令绑定到Loaded事件,则应使用“System.Windows.Interactivity”程序集。

<UserControl x:Class="Components.Map.MapView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:map="clr-namespace:Components.Map"
             xmlns:controls="clr-namespace:Windows.Controls;assembly=Windows.Controls"
             xmlns:ValidationRules="clr-namespace:Windows.Controls.ValidationRules;assembly=Windows.Controls"
             xmlns:directGraphicsControl="clr-namespace:Windows.DirectGraphicsControl;assembly=Windows.DirectGraphicsControl"
             xmlns:colorBar="clr-namespace:Components.Common.ColorBar;assembly=Components.Common"
             xmlns:RefinedRibbonControls="clr-namespace:Components.Common.Controls.RefinedRibbonControls;assembly=Components.Common"
             xmlns:UserControls="clr-namespace:Components.Common.UserControls;assembly=Components.Common"
             xmlns:map1="clr-namespace:Models.Map;assembly=Models.Map"
             xmlns:utilities="clr-namespace:Windows.Utilities;assembly=Windows.Utilities"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">

             <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:InvokeCommandAction Command="{Binding LoadedCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>

</UserControl>

System.Windows.Interactivity.dll位于Microsoft表达式混合软件开发工具包(SDK)(download link)中,也可以NuGet package的形式提供。

ubbxdtey

ubbxdtey2#

1.安装nuget软件包Microsoft.Xaml.Behaviors.Wpf(系统.Windows.交互性已弃用)
1.添加xmlns到窗口/用户控件。xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
1.将触发器添加到主窗口

<b:Interaction.Triggers>
        <b:EventTrigger EventName="Loaded">
            <b:InvokeCommandAction
                CommandParameter="{Binding}"
                Command="{Binding MyLoadedCommand}"/>
        </b:EventTrigger>
    </b:Interaction.Triggers>

相关问题