将命令绑定到视图的加载事件 [英] Bind command to Loaded event of view
问题描述
我正在尝试获取一种在视图加载完成后运行的方法.我试图将命令绑定到视图中的 Loaded
事件,但它无法运行.抛出的内部异常是
'在'System.Windows.Data.Binding' 上提供值引发异常.'行号 '14' 和行位置 '14'
如何绑定到视图的 Loaded
事件,以便在视图加载完成后运行某些内容?
如果要将命令绑定到 Loaded
事件,则应使用System.Windows.Interactivity"程序集.
>
<i:Interaction.Triggers><i:EventTrigger EventName="已加载"><i:InvokeCommandAction Command="{Binding LoadedCommand}"/></i:EventTrigger></i:Interaction.Triggers></用户控件>
System.Windows.Interactivity.dll 位于 Microsoft Expression Blend 软件开发工具包 (SDK) (下载链接),也可作为 NuGet 包.
I am trying to get a method to run when a view has finished loading. I have tried to bind a command to the Loaded
event in the view but it fails to run. The inner exception that is thrown is
'Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '14' and line position '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}">
How am I able to bind to a view’s Loaded
event so I can run something after the view has finished loading?
If you want to bind command to the Loaded
event, you should use the "System.Windows.Interactivity" assembly.
<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 is in Microsoft Expression Blend Software Development Kit (SDK) (download link) and also available as a NuGet package.
这篇关于将命令绑定到视图的加载事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!