Search code examples
c#wpfinteropwindow-handles

Problem getting the window handle of WPF application


I'm currently struggeling in getting the window handle of my WPF application. Here is a code snippet of the App.xaml.cs:

        _Logger.Info("Creating main window and view model.");

        MainWindow mainWindow = new MainWindow();

        _Logger.Info("MainWindow created");
        try
        {
            FrameworkAdjustments.WindowHandle = new WindowInteropHelper(mainWindow).EnsureHandle();
        }
        catch (Exception ex)
        {
            _Logger.Error("Could not ensure WindowHandle", ex);
        }

        _Logger.Info("WindowHandle created");
        //...
        var viewModel = new MainWindowViewModel();
        mainWindow.Initialize(viewModel);
        mainWindow.WindowState = WindowState.Maximized;

        mainWindow.Show();

In very less cases, the application stops inside the try. I get the logs "Creating main window and view model." and "MainWindow created", but nothing else. Neither the error, nor the "WindowHandle created".

As said, this is not reproducable on any other machine, than on the one of a special customer. How can I solve this issue? I need to get the window handle, to create an other neccessary class.

Thanks in advance

Edit 1: Added last 5 lines of code.


Solution

  • Hi all and thanks for your replys. I now found the issue and wanted to tell you, if anybody else runs into a similar problem. The .NET Framework was not installed correctly. I've uninstalled and reinstalled and everything works fine now.