从主窗口打开时,WPF-Window 的 C#/.NET 构造函数被调用两次

问题描述 投票:0回答:1

我有一个应用程序,它以集成到默认主窗口中的主菜单开始。这工作得很好,但现在我必须创建一个自动数据更新函数,该函数应该在应用程序显示主窗口之前更新数据。

这也很好用,但我注意到在加载/初始化主窗口时,我的更新窗口被调用了两次,因此更新函数也被称为两次。应用程序还运行被调用的 Autoupdater-Window 的构造函数两次。

MainWindow.xaml.cs的代码:

public MainMenuView()
{
    // Instance needed classes
    DatabaseLock _databaseLock = new DatabaseLock();
    Utils _util = new Utils();
    Eventlog _eventlog  = new Eventlog();

    // Check if auto update had already run today
    if (Settings.Default.LastJSONUpdate.Date == DateTime.Now.Date)
    {
        // Write error to the eventlog
        _eventlog.WriteEventLog("MainMenuView()->MainMenuView.xaml.cs", "foo", _util.GetCurrentUser(), DateTime.Now, Eventlog.Types.INFO);
        // Show info message to the user
        System.Windows.Forms.MessageBox.Show("Dataupdate already runned today!", "foo", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        // Create lock for table
        _databaseLock.CreateLock("autoupdate", _util.GetCurrentUser());

        // Check if the autoupdate-window is already open and if not open the updater
        if (!_util.CheckIfWinowIsOpened("AutoUpdate"))
        {
            // Minimize the mainmenu window
            _util.MinimizeMaximizeMainMenu();

            // Create new instance of AutoUpdaterView
            View.AutoUpdaterView autoUpdaterView = new View.AutoUpdaterView();
            // Show the window
            autoUpdaterView.Show();

            // Disable the mainwindow
            System.Windows.Application.Current.Windows[0].IsEnabled = false;
        }
    }

    InitializeComponent();
}

我尝试将代码移动到 UserControl 本身的 cs-File 中,但这没有帮助,结果是相同的。

所以我希望有人能给我的想法带来一些启发。

提前致谢 地理编码器

c# .net .net-4.0
1个回答
0
投票

我已经尝试了评论中建议的所有解决方案。

对我有用的解决方案是为更新屏幕创建一个自己的用户控件,并通过主窗口的弹出窗口显示它。现在所有消息仅加载一次。

感谢您的所有建议和意见!

© www.soinside.com 2019 - 2024. All rights reserved.