本文介绍了从winform打开wpf抛出未设置为实例的对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 自定义控件,我需要从 WinForm 打开它.我已按照 http://weblogs.asp 中提到的所有步骤进行操作.net/jdanforth/open-a-wpf-window-from-winforms在WindowsForm APP中打开WPF窗口

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 自定义控件:

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;
                }
            }
        }
    }
}

感谢任何支持.

推荐答案

Application.Current 实际上是特定于 WPF 应用程序的.所以我认为由于您尝试从 WinForms 应用程序打开 WPF 应用程序,您需要在访问它之前先初始化 WPF 应用程序的实例.

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();
}

如果这不起作用,请尝试在 WPF 窗口的加载事件中设置 Application.Current.MainWindow = this;.

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

这应该可以解决问题.

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

}

这篇关于从winform打开wpf抛出未设置为实例的对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:39