0
votes

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.

2
Do you need this already here or could this be moved to the Loaded event handler of the MainWindow?Klaus Gütter
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.Sinatr
@Klaus Gütter: Yes, I need that here, beacuse after that snippet, there is created a DA-Server, which needs the handle.Benny
@Sinatr: Unfortunately there is no entry in the windows logBenny
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.Andy

2 Answers

1
votes

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();
0
votes

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.