3 Replies - 574 Views - Last Post: 10 October 2012 - 12:17 PM Rate Topic: -----

#1 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Error running Embedded WPF to windows form

Posted 09 October 2012 - 11:09 PM

I embedded the WPF to vb.net and I got some error.
1. There's still a green underline on line
Me.SampleUserControl1 = New SampleWpfUserControlLibrary.SampleUserControl()
but that's nothing I guess since the underline was green.
2. My very problem is on the line
_hostwindow.OpenDocument();
says

Quote

Object reference not set to an instance of an object.

Posted Image
My guess is that, it was suppose to be hosted on a Window but in my project's case, it was hosted on an Elementhost

Here's the code :
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace SampleWpfUserControlLibrary
{
    /// <summary>
    /// Interaction logic for SampleUserControl.xaml
    /// </summary>
    public partial class SampleUserControl
    {
        private IApplicationHostWindow _hostWindow;
        
        [Obsolete("This constructor should not be used", false)]
        public SampleUserControl()
        {
            InitializeComponent();
        }

        public SampleUserControl(IApplicationHostWindow applicationHostWindow)
        {
            _hostWindow = applicationHostWindow;

            IsHitTestVisible = true;

            InitializeComponent();
        }

        public double GetViewboxWidth()
        {
            return viewHost.ActualWidth;
        }

        public double GetViewboxHeight()
        {
            return viewHost.ActualHeight;
        }

        public void SetViewboxChild(UIElement child)
        {
            viewHost.Content = child;
        }

        public void ProcessLeftButtonDown(Point point)
        {
            var result = VisualTreeHelper.HitTest(this, point);
            return;
        }

        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            return base.HitTestCore(hitTestParameters) ??
                   new PointHitTestResult(viewHost.Content as Visual ?? viewHost, hitTestParameters.HitPoint);
        }

        private void exitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            _hostwindow.Exit();
        }

        private void newProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
             _hostwindow.OpenDocument();
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            return;
        }

        private void UserControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            return;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error running Embedded WPF to windows form

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Error running Embedded WPF to windows form

Posted 10 October 2012 - 12:45 AM

The green underline comes from the fact that you are using obsolete constructor, as it says on line 15 of the SampleUserControl class. You should use the constructor on line 21 instead which takes the IApplicationHostWindow as a parameter. That constructor sets the parameter to _hostWindow, which is throwing the error you are receiving at the moment. The obsolete constructor you use doesn't initialize the _hostWindow member.

So to fix both problems, use other constructor and pass IApplicationHostWindow to it.
Was This Post Helpful? 2
  • +
  • -

#3 k3nnt0ter0  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 181
  • Joined: 16-January 10

Re: Error running Embedded WPF to windows form

Posted 10 October 2012 - 03:35 AM

I placed the Constructor on Line 21 and Yes, the green line disappeared.
I still got same error being null of _hostwindow. .

Could you help me about this

Quote

So to fix both problems, use other constructor and pass IApplicationHostWindow to it.

It works fine when I run WPF without adding it on vb.net. . Error just occurs when adding the WPF Project on VB.Net
Posted Image

This post has been edited by k3nnt0ter0: 10 October 2012 - 04:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5428
  • View blog
  • Posts: 11,640
  • Joined: 02-June 10

Re: Error running Embedded WPF to windows form

Posted 10 October 2012 - 12:17 PM

View Postk3nnt0ter0, on 10 October 2012 - 04:35 AM, said:

I embedded the WPF to vb.net
...
Error just occurs when adding the WPF Project on VB.Net


Maybe its a language barrier issue, but this makes no sense.

Your code is in C#. What are you trying to do with VB.NET?

Quote

Me.SampleUserControl1 = New SampleWpfUserControlLibrary.SampleUserControl()


Just because both C# and VB.NET are both .NET languages doesn't mean you can use both languages in the same file/class. You can't put some lines in C# and some lines in VB. Would you write a book with some lines in English and some lines in Russian and expect the reader to understand it all?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1