简体   繁体   English

如何从App.xaml.cs运行MainWindow_Loaded?

[英]How to run MainWindow_Loaded from App.xaml.cs?

I have a WPF app, in file Main.xaml.cs I have the following constructor: 我有一个WPF应用程序,在文件Main.xaml.cs我具有以下构造函数:

   public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

from another class: 从另一类:

In App.xaml.cs

I need to fire an event which will make run method MainWindow_Loaded in Main.xaml.cs 我需要触发一个事件,该事件将使Main.xaml.cs中的运行方法MainWindow_Loaded

Any idea how to do it? 知道怎么做吗?

You can do this by manually creating the MainWindow in your App class. 您可以通过在App类中手动创建MainWindow来实现。 To do it, remove the StartUp attribute from the App.xaml so that it looks like this... 为此,请从App.xaml中删除StartUp属性,使其看起来像这样...

<Application x:Class="Anything.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             >
</Application>

In your App.xaml.cs class, override the OnStartup method like this... 在您的App.xaml.cs类中,重写OnStartup方法,如下所示:

  public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MainWindow mw = new MainWindow();
            mw.Loaded += mw_Loaded;
            mw.Show();
        }
        void mw_Loaded(object sender, RoutedEventArgs e)
        {   // loaded event comes here
            throw new NotImplementedException();
        }
    }

This override manually creates the MainWindow and shows it. 此替代手动创建MainWindow并显示它。 It also subscribes to the Loaded event and receives the notification in the mw_Loaded method. 它还订阅Loaded事件,并在mw_Loaded方法中接收通知。 You can also call the window's method directly because you have the window instance. 您也可以直接调用窗口的方法,因为您具有窗口实例。

Alternatively, you can overload the MainWindow constructor and pass it an Action delegate. 或者,您可以重载MainWindow构造函数并向其传递Action委托。 It would look like this... 看起来像这样...

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MainWindow mw = new MainWindow(DoSomething);
        mw.Show();
    }
    public void DoSomething()
    {
    }
}

And the MainWindow would look like this... 而且MainWindow看起来像这样...

public partial class MainWindow
{
    private readonly Action _onLoaded;
    public MainWindow(Action onLoaded)
    {
        _onLoaded = onLoaded;
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }
    void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        _onLoaded();
    }
}

That gives you two alternatives, there are other ways also, but these are the most expedient. 这给了您两种选择,也有其他方法,但这是最方便的。 As Sheridan pointed out, tinkering with a window's loaded event can have confounding side effects, like re-entrancy. 正如Sheridan指出的那样,修改窗口的加载事件可能会产生令人困惑的副作用,例如重新进入。 The WPF forefathers envisioned it as a lifetime event. WPF的祖先将其视为一生的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM