在加载时执行Silverlight 4命令

时间:2010-04-22 20:33:04

标签: silverlight mvvm silverlight-4.0

如何在用户控件加载时执行Silverlight 4命令而不是映射到显式按钮单击?

3 个答案:

答案 0 :(得分:4)

或者只需在xaml中为您的UserControl添加trigger

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <si:InvokeDataCommand Command="{Binding MyCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

答案 1 :(得分:1)

创建DependencyProperty类型ICommand: -

    #region public ICommand LoadedCommand

    public ICommand LoadedCommand
    {
        get { return GetValue(LoadedCommandProperty) as ICommand; }
        set { SetValue(LoadedCommandProperty, value); }
    }

    public static readonly DependencyProperty LoadedCommandProperty =
            DependencyProperty.Register(
                    "LoadedCommand",
                    typeof(ICommand),
                    typeof(MainPage),
                    new PropertyMetadata(null));

    #endregion public ICommand LoadedCommand

还添加一些内容作为命令参数: -

    #region public object LoadedCommandParameter

    public object LoadedCommandParameter
    {
        get { return GetValue(LoadedCommandParameterProperty) as object; }
        set { SetValue(LoadedCommandParameterProperty, value); }
    }

    public static readonly DependencyProperty LoadedCommandParameterProperty =
            DependencyProperty.Register(
                    "LoadedCommandParameter",
                    typeof(object),
                    typeof(MainPage),
                    new PropertyMetadata(null));

    #endregion public object LoadedCommandParameter

现在按如下方式设置执行: -

    public UserControl1()
    {
        InitializeComponent();
        Loaded += UserControl1_Loaded;
    }

    void UserControl1_Loaded(object sender, RoutedEventArgs e)
    {
        if (LoadedCommand != null && LoadedCommand.CanExecute(LoadedCommandParameter))
        {
            LoadedCommand.Execute(LoadedCommandParameter);
        }
    }

现在,如果你的ViewModel(有一个名为StartStuff的命令),那么: -

  <UserControl1 LoadedCommand="{Binding StartStuff}" .... >

答案 2 :(得分:1)

这是一堆代码。没有代码的简洁版

public class LoadedBehaviour
{
   public static ICommand GetLoadedCommand(DependencyObject dependencyObject)
   {
      return (ICommand)dependencyObject.GetValue(LoadedCommandProperty);
   }

   public static void SetLoadedCommand(DependencyObject dependencyObject, ICommand value)
   {
      dependencyObject.SetValue(LoadedCommandProperty, value);
   }

   public static Action GetLoadedCommandExecutor(DependencyObject dependencyObject)
   {
      return (Action)dependencyObject.GetValue(LoadedCommandExecutorProperty);
   }

   public static void SetLoadedCommandExecutor(DependencyObject dependencyObject, Action value)
   {
      dependencyObject.SetValue(LoadedCommandExecutorProperty, value);
   }

   public static readonly DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(FrameworkElement), new PropertyMetadata(OnPropertyChanged));
   public static readonly DependencyProperty LoadedCommandExecutorProperty = DependencyProperty.Register("LoadedCommandExecutor", typeof(Action), typeof(FrameworkElement), new PropertyMetadata(OnPropertyChanged));

   private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      if (!(d is FrameworkElement))
      {
         throw new ArgumentException("Loaded command can only be used on FrameworkElements");
         var executor = GetLoadedCommandExecutor(d);
         if(executor == null)
         {
            executor = () =>
            {
                   var command = GetLoadedCommand(d);
                   command.Execute(e);
         };
            SetLoadedCommandExecutor(d, executor);
            ((FrameworkElement)d).Loaded += (obj, args) => executor();
      }
   }
}