0

I have a WPF custom control and I needs to open it from WinForm. I have followed all steps mentioned in http://weblogs.asp.net/jdanforth/open-a-wpf-window-from-winforms and Open WPF window in WindowsForm APP

But still it gives me an object reference not set to an instance of exceptions.

Winform:

private void button1_Click(object sender, EventArgs e)
        {
            var notificatioinapp = new WpfCustomControlLibrary1.Window1();
            ElementHost.EnableModelessKeyboardInterop(notificatioinapp);
            notificatioinapp.Show();
        }

WPF custom control:

public partial class Window1 : Window
    {
        public Window1() : base()
        {
            InitializeComponent();
            this.Closed += this.NotificationWindowClosed;
        }
    public new void Show()
    {
        this.Topmost = true;
        base.Show();

        this.Owner = System.Windows.Application.Current.MainWindow;
        this.Closed += this.NotificationWindowClosed;
        var workingArea = Screen.PrimaryScreen.WorkingArea;

        this.Left = workingArea.Right - this.ActualWidth;
        double top = workingArea.Bottom - this.ActualHeight;

        foreach (Window window in System.Windows.Application.Current.Windows)
        {
            string windowName = window.GetType().Name;

            if (windowName.Equals("NotificationWindow") && window != this)
            {
                window.Topmost = true;
                top = window.Top - window.ActualHeight;
            }
        }

        this.Top = top;
    }
    private void ImageMouseUp(object sender,
        System.Windows.Input.MouseButtonEventArgs e)
    {
        this.Close();
    }

    private void DoubleAnimationCompleted(object sender, EventArgs e)
    {
        if (!this.IsMouseOver)
        {
            this.Close();
        }
    }

    private void NotificationWindowClosed(object sender, EventArgs e)
    {
        foreach (Window window in System.Windows.Application.Current.Windows)
        {
            string windowName = window.GetType().Name;

            if (windowName.Equals("NotificationWindow") && window != this)
            {
                // Adjust any windows that were above this one to drop down
                if (window.Top < this.Top)
                {
                    window.Top = window.Top + this.ActualHeight;
                }
            }
        }
    }
}

Appreciate any support.

Community
  • 1
  • 1
helloworld
  • 965
  • 5
  • 14
  • 34
  • is the null reference hit on object **notificatioinapp** ? – ViVi Jun 21 '16 at 04:29
  • no its coming from this.Owner = System.Windows.Application.Current.MainWindow; in Show() method of the WPF part. I tried assigning this ref to above MainWindow inside the constructor, but it didn't work. – helloworld Jun 21 '16 at 04:30
  • So are you trying to open the wpf application in the same window? – ViVi Jun 21 '16 at 04:38
  • Yes, I am try to open a WPF custom control from a Windows from applications. Conceptually something similar to a toast or a balloon notifications. – helloworld Jun 21 '16 at 04:41

1 Answers1

4

Application.Current is Specific for WPF Application actually. So I think since you are trying to open WPF application from WinForms Application you need to initialize instance of WPF Application first before accessing it.

if ( null == System.Windows.Application.Current )
{
   new System.Windows.Application();
}

If this doesn't work try setting Application.Current.MainWindow = this; in loaded event of WPF window.

This should do the fix.

EDIT :

private void button1_Click(object sender, EventArgs e)
{

    if (null == System.Windows.Application.Current)
    {
        new System.Windows.Application();
    }

    var wpfwindow = new Window();
    wpfwindow = new WpfCustomControlLibrary1.Window1();
    ElementHost.EnableModelessKeyboardInterop(wpfwindow);
    wpfwindow.Show();

}
ViVi
  • 4,339
  • 8
  • 29
  • 52
  • This worked. You rock bro! Thanks a lot. is it ok to initialize this inside the constructor? and are there any possible complications due to manually initializing the Application instance? – helloworld Jun 21 '16 at 04:49
  • Somehow this threw the following exception "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed" when I attempt to open the WPF app for the second time – helloworld Jun 21 '16 at 04:54
  • Lol. There is no issue like that. But better to initialize this in loaded event, as I have done like that. If it's working fine in constructor without any issues, then leave it like that. It's not manually initializing, it belongs to **WPF**. You need to initialize it if you're doing such a transition. Even I have had such a scenario once. So I came to know all this. – ViVi Jun 21 '16 at 04:55
  • Answer to 2nd comment : Yes that is why I told you to handle loaded event and do that in the loaded event since loaded event is handled always. Or else do while transiting to the application. – ViVi Jun 21 '16 at 04:57
  • I tried handling it in both loaded and initialized events but no luck. base.Show(); this throws the exception – helloworld Jun 21 '16 at 05:04
  • :) Hey.. Any luck? – helloworld Jun 21 '16 at 05:57
  • 1
    I have edited the answer. Sorry I am in a hurry. Works fine here. Check for more error scenarios anyway. No need to handle loaded event. This itself is fine. – ViVi Jun 21 '16 at 06:22