6

I am using WPF. I have a static class that performs some setup not available during design mode. This constructor gets called by a window in design mode, which results in an exception being thrown.

How do I detect design mode in a static method, so I can invoke the appropriate design mode behavior?

The recommended approach does not work for static methods.


Edit:

The static constructor is called from xaml, so I can't conditionally call it (unless I move the call to code-behind, which I'd like to avoid).

In the window: <Window ... HelpProvider.Keyword="some_help_topic.html">

In the class:

static HelpProvider()
{
    // Load the .chm file from an application setting (this fails at design time)

    // Add a WPF command binding
}
3
  • Check design mode in your window and determine if you need to call you static setup method there. Commented Oct 16, 2012 at 15:50
  • In case this doesn't work for you, provide a code snippet you're trying to make work, please. Commented Oct 16, 2012 at 15:51
  • Thought I had it but didn't see you want to no use code behind and that you had such strange conditions. Sorry. Recommended to use DesignerProperties.GetIsInDesignMode(Application.Current.MainWindow), but in fact I don't know if this works for your strange situation. Commented Oct 16, 2012 at 16:39

1 Answer 1

4

The possible way to solve it keeping attached property in xaml file is:

  1. Move initialization code from static constructor to attached property changed callback. Frankly speaking, it is not good practice to do such kind of work in static constructors.
  2. In your attached property changed callback you have a reference to your window. So you can call DesignerProperties.GetIsInDesignMode(yourwindow) there and decide, if you need to load file or whatever causes issues.
Sign up to request clarification or add additional context in comments.

1 Comment

Nice. I hadn't managed to work out how to move the initialization code out of the static constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.