C# 窗口加载和 WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12589772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:49:56  来源:igfitidea点击:

Window Loaded and WPF

c#wpfeventsxamlmvvm

提问by Dirk Dastardly

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:

我在 Windows 2012 中有一个 WPF 项目,我需要在 Window Loaded 事件中加载一些信息。不过,我需要在视图模型中而不是在 CodeBehind 中执行此操作。我正在尝试使用以下代码:

In my xaml:

在我的 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?

OnAttached、AssociatedObject_Loaded 和 LoadedCommand get 都在触发,但 LoadedCommand 集没有触发,显然,WindowLoadedCommand 没有触发。任何线索我能做些什么来让它工作?

采纳答案by Harlow Burgess

There are a few options. A couple of them listed here:

有几个选项。这里列出了其中的几个:

how to call a window's Loaded event in WPF MVVM?

如何在 WPF MVVM 中调用窗口的 Loaded 事件?

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.

但是,如果您或其他任何人担心您花费几个小时来完成本应花费 30 秒的任务,您可能想尝试一下。

public MainWindow()
{
    InitializeComponent();

    this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    ShellViewModel.Instance.WindowLoadedCommand.Execute(null);
}