1

I am developing a WPF app which shows constantly updating values (X, Y, Z) from an accelerometer. When I work with the XAML to modify the window or even just to look at it in the designer, it shows the live stream of data constantly updating as if the program were running. Or rather, because the program is running (somehow - I don't know how).

Nifty as this is, it is causing the following problem. When I then really do run the program, I get all zeroes from the device. It seems like Visual Studio is consuming the data and will not let it get to my window. In order to make it stop, I have to

1. activate a different file in the editor (or just close the .xaml file), then
2. close and reopen visual studio.

Is there a better way to keep Visual Studio from doing this?


Edit Based on the answer offered by SLaKs, I have added the following:

I am now checking for the value of DesignerProperties.GetIsInDesignMode(this) as suggested, but a different problem now crops up. My MainWindow constructor now looks like this:

  public MainWindow()
  {
     InitializeComponent();

     var dc = this.TopGrid.DataContext as RawAccelStream_ViewModel;
     if (DesignerProperties.GetIsInDesignMode(this) == false)
        base.Background = Brushes.Bisque;
     else
     {
        base.Background = Brushes.Blue;
        dc.Dispose();
     }
  }

The new problem is that with this code, when running the application the window background appears Bisque, but no color change happens at all in Designer View. And Designer View still shows values and still spoils the application instance of the window.

philologon
  • 2,093
  • 4
  • 19
  • 35

1 Answers1

0

When you open a XAML file that uses a control in the designer, the designer will create an actual instance of that control.

You should modify your control to check whether it's running in the designer and show fake data or something.

Check DesignerProperties.GetIsInDesignMode(this).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • And if that is true, what? Set DataContext = null? And do this in the MainWindow.xaml.cs file? – philologon Nov 04 '14 at 22:36
  • The designer will only create an instance of controls you create in XAML; not of the class for that file. – SLaks Nov 04 '14 at 22:49
  • If that is true, simply don't run the code that's bothering you. Optionally, you can use a fake data source instead so that the designer looks nice. – SLaks Nov 04 '14 at 22:50
  • I tried this suggestion, but it did not ultimately stop the Designer View from updating. I put the explanation in my OP as an Edit. – philologon Nov 05 '14 at 01:35
  • @philologon: Find the code that is doing what you don't want and wrap it in an `if` statement. You can open a second copy of VS and attach its debugger to `XDesProc.exe` to debug the designer and find out what code is running. – SLaks Nov 05 '14 at 03:46
  • In a constructor, you may need to check http://stackoverflow.com/a/2000791/34397 instead. – SLaks Nov 05 '14 at 03:47
  • "what is running" is a Reactive Extension hot observable: the accelerometer data stream. That is why I call Dispose on my ViewModel -- to shut down the data stream. I.e., the Brushes.Blue code block does not get executed. So the problem is not that it does not adequately check for design mode. The problem is that /my/ constructor is not even being called when it is in Design Mode. I know because I never see a blue background. So I don't know where to intercept it and shut down the hot observable data stream. – philologon Nov 05 '14 at 20:35
  • IIRC, `GetIsInDesignMode(this)` will not work correctly in a ctor. Try checking http://stackoverflow.com/a/2000791/34397 instead. – SLaks Nov 05 '14 at 20:48