Null reference when using element host.

Dec 3, 2010 at 5:43 AM

I am hosting a WPF control in a winform ap.

I am trying to create my root view (using caliburn) and add it to the ElementHost.

My code goes something like this


    public partial class WinForm
    {
        public WinForm()
        {
            InitializeComponent();
            var autofacBootStrapper = new AutofacBootStrapper();
            autofacBootStrapper.Configure();

            var viewModel = IoC.Get<MyViewModel>();
            var element = ViewLocator.LocateForModel(viewModel, null, null);
            ViewModelBinder.Bind(viewModel, element, null);

            elementHost1.Child = element;
        }
    }

The problem is that ViewModelBinder.Bind eventually calls Bootstrapper.IsInDesignMode and Application.Current.ToString(); throws a null reference.

My temporary fix was to modify Caliburn.Micro to use IsInDesignMode from MVVM Light 

http://mvvmlight.codeplex.com/SourceControl/changeset/view/3bdbffb4e70a#GalaSoft.MvvmLight%2fGalaSoft.MvvmLight%20%28NET35%29%2fViewModelBase.cs

Am I doing it wrong ? :)

Some other relevant posts

http://geekswithblogs.net/lbugnion/archive/2009/09/05/detecting-design-time-mode-in-wpf-and-silverlight.aspx
http://caliburn.codeplex.com/Thread/View.aspx?ThreadId=60114
http://caliburn.codeplex.com/Thread/View.aspx?ThreadId=83275

 

Coordinator
Dec 3, 2010 at 2:57 PM

I'll add this as a bug. Detecting design mode on all platforms for all tools is way more difficult than it should be...

Dec 7, 2010 at 11:24 AM

I've also just run into this issue when running unit tests from Visual Studio, Application.Current is null.  Should the dependency on Application be abstracted and injected so that it can be mocked in unit tests as required?

Coordinator
Dec 7, 2010 at 3:06 PM

Fixed in d159a8bddd0e 

Dec 7, 2010 at 11:13 PM
Thanks Rob

On Wed, Dec 8, 2010 at 2:06 AM, EisenbergEffect
<notifications@codeplex.com> wrote:
> From: EisenbergEffect
>
> Fixed in d159a8bddd0e
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed]@discussions.codeplex.com)
>
> To start a new discussion for this project, email
> [email removed]@discussions.codeplex.com
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe on CodePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at CodePlex.com