WPF is determined whether the object is a window type of design, the window instead of the runtime

Original: WPF window to determine whether an object is of type design, window instead of runtime

When we Windowwrite an additional attribute types when the property change notification, we need to determine whether the object is dependent on a window. If, however, directly determine whether it is Windowthe type, then set this property will direct an exception in the designer.

So there is no way we can know this is a window design it? So it will not throw an exception, and can be the perfect support for the designer.


Method a: design-time property is determined

WPF carrying a native additional attributes can determine whether a design is derived from the dependent objects. And this attribute is DesignerProperties.IsInDesignMode.

In WPF designer, this property is rewritten designed metadata specifying a value true, and other default, it is the default value false.

Therefore, by determining this value can be known at this time whether this additional properties in the designer.

if (DesignerProperties.GetIsInDesignMode(d))
{
    // 通常我们考虑在设计器中不做额外的任何事情是最偷懒不会出问题的代码了。
    return;
}

   
   
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

I've used this method to judge in these blog, you can see the source code:

Method two: Analyzing the design window

The above method is a method of determining a common designer. However, if we want to get more design support, rather than direct as above, returncauses this property in the designer results that are not, then we need a more accurate determination.

However, the designer of the type we can not directly refer to, so you can type the name of judge considered way. Type the name of judgment way will be related to the version of Visual Studio, so this is actually how the code is not good-looking.

I will judge the method are summarized as follows:

public static class WalterlvDesignTime
{
    /// <summary>
    /// 判断一个依赖对象是否是设计时的 <see cref="Window"/>。
    /// </summary>
    /// <param name="window">要被判断设计时的 <see cref="Window"/> 对象。</param>
    /// <returns>如果对象是设计时的 <see cref="Window"/>,则返回 true,否则返回 false。</returns>
    private static bool IsDesignTimeWindow(DependencyObject window)
    {
        const string vs201920172015Window =
            "Microsoft.VisualStudio.DesignTools.WpfDesigner.InstanceBuilders.WindowInstance";
        const string vs2013Window = "Microsoft.Expression.WpfPlatform.InstanceBuilders.WindowInstance";

        if (DesignerProperties.GetIsInDesignMode(window))
        {
            var typeName = window.GetType().FullName;
            if (Equals(vs201920172015Window, typeName) || Equals(vs2013Window, typeName))
            {
                return true;
            }
        }

        return false;
    }
}

   
   
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

So, just what you need to call this method to get the window instance is whether the design of the window:

if (WalterlvDesignTime.IsDesignTimeWindow(d))
{
    // 检测到如果是设计时的窗口,就跳过一些句柄等等一些真的需要一个窗口的代码调用。
}
else if (d is Window)
{
    // 检测到真的是窗口,做一些真实窗口初始化需要做的事情。
}
else
{
    // 这不是一个窗口,需要抛出异常。
}

   
   
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

My blog will be starting in https://blog.walterlv.com/ , and will be released from CSDN which featured, but it is rarely updated once released.

If you see any do not understand the contents of the blog, please share. I built dotnet Vocational and Technical College welcome to join.

Creative Commons License

This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Lu Yi (containing links: https://walterlv.blog.csdn.net/ ), shall not be used for commercial purposes, a work based on the article be sure to modify the same license release. If you have any questions, please contact me .

Published 382 original articles · won praise 232 · views 470 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12051944.html