2010-04-22 88 views

回答

1

创建类型的DependencyPropertyICommand: -

#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}" .... > 
4

或者干脆在XAML添加trigger你的用户控件:

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

这很好用,但它不那么简单,您需要添加适当的引用,从而增加应用程序的下载大小。如果这些参考文献有其他各种用途,但如果不是这样,那么编码解决方案就不需要为此要求提供额外参考。 – AnthonyWJones 2010-04-23 06:43:00

+0

确实,您必须引用System.Windows.Interactivity(45K)和Expression.Samples.Interactivity(53K)才能使其工作。 – 2010-04-23 14:32:11

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