Running Silverlight 4 command on boot

How do you implement the Silverlight 4 command to execute when user control loads, rather than being displayed on an explicit button?

+3
source share
3 answers

Or just add trigger in xaml for your UserControl:

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

Create DependencyPropertyType 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

Also add something to act as a command parameter: -

    #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

Now configure its execution as follows: -

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

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

Now, if your ViewModel (has a command called StartStuff), then:

  <UserControl1 LoadedCommand="{Binding StartStuff}" .... >
+1
source

.

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();
      }
   }
}
+1

Source: https://habr.com/ru/post/1742343/


All Articles