窗口加载和WPF [英] Window Loaded and WPF
问题描述
在我的xaml:
<交互:Interaction.Behaviors>
<行为:WindowLoadedBehavior LoadedCommand ={Binding WindowLoadedCommand}/>
< / interactivity:Interaction.Behaviors>
在我的查看模型中:
private DelegateCommand _WindowLoadedCommand;
public DelegateCommand WindowLoadedCommand
{
get
{
return _WindowLoadedCommand;
}
private set
{
_WindowLoadedCommand = value;
}
}
public ShellViewModel()
{
WindowLoadedCommand = new DelegateCommand(WindowLoadedAction);
}
protected void WindowLoadedAction()
{
...
}
我附加的行为:
public class WindowLoadedBehavior:Behavior< FrameworkElement>
{
[SuppressMessage(Microsoft.StyleCop.CSharp.MaintainabilityRules,SA1401:FieldsMustBePrivate,Justification =Dependency Property。Allow public)]
public static DependencyProperty LoadedCommandProperty = DependencyProperty 。注册(LoadedCommand,typeof(ICommand),typeof(WindowLoadedBehavior),新的PropertyMetadata(null));
public ICommand LoadedCommand
{
get {return(ICommand)GetValue(LoadedCommandProperty); }
set {SetValue(LoadedCommandProperty,value);
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded + = AssociatedObject_Loaded;
}
protected override void OnDetaching()
{
AssociatedObject.Loaded - = AssociatedObject_Loaded;
base.OnDetaching();
private void AssociatedObject_Loaded(object sender,RoutedEventArgs e)
{
if(LoadedCommand!= null)
LoadedCommand.Execute(null);
}
}
OnAttached,AssociatedObject_Loaded和LoadedCommand get都被触发,但是LoadedCommand集没有触发,并且显然WindowLoadedCommand没有触发。任何线索我可以做些什么来使这项工作?
有几个选择。他们中的几个列在这里:
然而,在机会中你或任何其他人都在乎你花了几个小时来完成一个应该花了30秒的任务,你可能要试试这个。
public MainWindow()
{
InitializeComponent();
this.Loaded + = new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender,RoutedEventArgs e)
{
ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
}
I have a WPF project in Windows 2012 in which I need to load some information in the Window Loaded event. I need to do this in the View Model rather than in the CodeBehind, though. I am attempting to use the following code:
In my xaml:
<interactivity:Interaction.Behaviors>
<behaviors:WindowLoadedBehavior LoadedCommand="{Binding WindowLoadedCommand}" />
</interactivity:Interaction.Behaviors>
In my View Model:
private DelegateCommand _WindowLoadedCommand;
public DelegateCommand WindowLoadedCommand
{
get
{
return _WindowLoadedCommand;
}
private set
{
_WindowLoadedCommand = value;
}
}
public ShellViewModel()
{
WindowLoadedCommand = new DelegateCommand(WindowLoadedAction);
}
protected void WindowLoadedAction()
{
...
}
My attached behavior:
public class WindowLoadedBehavior : Behavior<FrameworkElement>
{
[SuppressMessage("Microsoft.StyleCop.CSharp.MaintainabilityRules", "SA1401:FieldsMustBePrivate", Justification = "Dependency Property. Allow public.")]
public static DependencyProperty LoadedCommandProperty = DependencyProperty.Register("LoadedCommand", typeof(ICommand), typeof(WindowLoadedBehavior), new PropertyMetadata(null));
public ICommand LoadedCommand
{
get { return (ICommand)GetValue(LoadedCommandProperty); }
set { SetValue(LoadedCommandProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
protected override void OnDetaching()
{
AssociatedObject.Loaded -= AssociatedObject_Loaded;
base.OnDetaching();
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
if (LoadedCommand != null)
LoadedCommand.Execute(null);
}
}
The OnAttached, AssociatedObject_Loaded and LoadedCommand get are all firing, but the LoadedCommand set is not firing and, obviously, the WindowLoadedCommand isn't firing. Any clue what I can do to get this working?
There are a few options. A couple of them listed here:
how to call a window's Loaded event in WPF MVVM?
However, in the off chance that you or anyone else cares that you are spending several hours to complete a task that should have taken 30 seconds, you might want to try this instead.
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
}
这篇关于窗口加载和WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!