1

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.

6
  • Do you need this already here or could this be moved to the Loaded event handler of the MainWindow? Commented Jun 15, 2020 at 5:56
  • If application crashes there is an event in windows logs and hopefully a dump. You may have to deal with those to find an answer. I'd first ensure what you handle everything. Commented Jun 15, 2020 at 6:13
  • @Klaus Gütter: Yes, I need that here, beacuse after that snippet, there is created a DA-Server, which needs the handle. Commented Jun 15, 2020 at 6:18
  • @Sinatr: Unfortunately there is no entry in the windows log Commented Jun 15, 2020 at 6:19
  • This seems a very odd place to put code that won't show mainwindow. I think your app won't really start until application.startup is finished and some sort of window starts. Automating a wpf app without it loading properly seems unlikely to work. But. I tried private void Application_Startup(object sender, StartupEventArgs e) { MainWindow mw = new MainWindow(); var handle = new WindowInteropHelper(mw).EnsureHandle(); Put a break point after that last line and it seems to work ok in visual studio. Commented Jun 15, 2020 at 7:45

2 Answers 2

2

You should create the WindowInteropHelper and access the handle after the SourceInitialized event has been raised:

MainWindow mainWindow = new MainWindow();
EventHandler eventHandler = null;
eventHandler = (ss, ee) =>
{
    mainWindow.SourceInitialized -= eventHandler;
    try
    {
        FrameworkAdjustments.WindowHandle = new WindowInteropHelper(mainWindow).EnsureHandle();
    }
    catch (Exception ex)
    {
        _Logger.Error("Could not ensure WindowHandle", ex);
    }
};
mainWindow.SourceInitialized += eventHandler;
...
mainWindow.Show();
Sign up to request clarification or add additional context in comments.

2 Comments

You can also call the WindowInteropHelper.EnsureHandle() method if you need the handle prior to initializing.
Thank you so much @radj307 - my WPF window is never shown and everything I tried resulted in a handle of 0. WindowInteropHelper.EnsureHandle() returns the handle if one exists, or creates one if it doesn't - it should be the accepted answer here.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.