如何在用户控件加载时执行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. Silverlight中的时序执行和加载
- 2. 与运行时执行命令(时)EXEC
- 3. 加载HTML - 命令行
- 4. 。从命令行加载pcre.so
- 5. 执行命令
- 6. 执行命令
- 7. 执行命令
- 8. 从NSIS执行命令行命令
- 9. 从Perl执行命令行命令?
- 10. 从命令行执行Django Shell命令
- 11. 从Python执行命令行命令
- 12. 从命令行执行jar时出错
- 13. mysql命令行返回执行时间?
- 14. PIG命令执行
- 15. 执行sql命令
- 16. 执行shell命令
- 17. PHP执行命令
- 18. JS执行命令
- 19. Verilog执行命令
- 20. SharpSsh命令执行
- 21. 执行命令bahaviour
- 22. SshNet命令执行
- 23. 执行symfony命令
- 24. 命令执行shell
- 25. Shell命令执行
- 26. 如何从命令行执行silverlight单元测试
- 27. 如何在启动时执行其他所有命令后执行命令?
- 28. 将命令行参数传递给OOB Silverlight 4应用程序
- 29. 无法加载应用程序或执行命令“Microsoft.AspNet.Hosting”
- 30. 有条件在文件加载在Emacs中执行命令24
这很好用,但它不那么简单,您需要添加适当的引用,从而增加应用程序的下载大小。如果这些参考文献有其他各种用途,但如果不是这样,那么编码解决方案就不需要为此要求提供额外参考。 – AnthonyWJones 2010-04-23 06:43:00
确实,您必须引用System.Windows.Interactivity(45K)和Expression.Samples.Interactivity(53K)才能使其工作。 – 2010-04-23 14:32:11