web-dev-qa-db-fra.com

Lier la commande à l'événement de vue chargé

J'essaie d'obtenir une méthode à exécuter lorsqu'une vue a terminé le chargement. J'ai essayé de lier une commande à l'événement Loaded dans la vue, mais il ne fonctionne pas. L'exception intérieure levée est

'Fournir de la valeur sur' System.Windows.Data.Binding 'a levé une exception.' Numéro de ligne "14" et position de ligne "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}">

Comment puis-je me lier à l'événement Loaded d'une vue pour pouvoir exécuter quelque chose une fois le chargement de la vue terminé?

30
scott lafoy

Si vous souhaitez lier la commande à l'événement Loaded, vous devez utiliser l'assembly "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 se trouve dans le Kit de développement logiciel (SDK) Microsoft Expression Blend ( lien de téléchargement ).

48
kmatyaszek