如何在用戶控件加載時執行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