4

I just created an empty WPF app in VS 2015.

It has

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var mainWindowHandle = new WindowInteropHelper(this).Handle;
    }
}

But mainWindowHandle is 0 always.

Is it OK? Should it be > 0 ?

AGB
  • 2,230
  • 1
  • 14
  • 21
NoWar
  • 36,338
  • 80
  • 323
  • 498

1 Answers1

7

Your window is not shown yet. So the actual window has not been created yet. Try examining this handle in Activated or Loaded event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        var mainWindowHandle = new WindowInteropHelper(this).Handle;
    }
}
filhit
  • 2,084
  • 1
  • 21
  • 34