如何在用户控件加载时执行Silverlight 4命令而不是映射到显式按钮单击时执行?加载时执行Silverlight 4命令
3
A
回答
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}" .... >
4
或者干脆在XAML添加trigger你的用户控件:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<si:InvokeDataCommand Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
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();
}
}
}
相关问题
- 1. 加载ReactiveUI执行命令
- 2. Silverlight中的时序执行和加载
- 3. 命令行命令的执行时间
- 4. 卸载后执行命令
- 5. 错误:No such column name.INFO:HHH000327:执行加载命令时出错:org.hibernate.exception.GenericJDBCException:
- 6. 当elFinder加载时运行命令
- 7. 命令行加载时编织
- 8. Silverlight的 - 命令犯规时执行复选框被点击
- 9. 与运行时执行命令(时)EXEC
- 10. 加载HTML - 命令行
- 11. 。从命令行加载pcre.so
- 12. 执行命令
- 13. 执行命令
- 14. 执行命令
- 15. 在加载时执行javascript
- 16. 从NSIS执行命令行命令
- 17. 从命令行执行Django Shell命令
- 18. 从Python执行命令行命令
- 19. 从Perl执行命令行命令?
- 20. C#运行命令不执行命令
- 21. 无法加载应用程序或执行命令'Microsoft.Dnx.TestHost'。可用命令:test
- 22. 使用hive -e'<hive命令>'执行配置单元加载命令'
- 23. 执行两个命令行同时
- 24. mysql命令行返回执行时间?
- 25. 在运行时执行java命令
- 26. Java - 运行时命令执行
- 27. 执行绿命令,同时运行(Raspbian)
- 28. 从命令行执行jar时出错
- 29. FDT:编译时执行命令行命令
- 30. 当启动cygwin时(从命令行)执行命令
这很好用,但它不那么简单,您需要添加适当的引用,从而增加应用程序的下载大小。如果这些参考文献有其他各种用途,但如果不是这样,那么编码解决方案就不需要为此要求提供额外参考。 – AnthonyWJones 2010-04-23 06:43:00
确实,您必须引用System.Windows.Interactivity(45K)和Expression.Samples.Interactivity(53K)才能使其工作。 – 2010-04-23 14:32:11