开发者

Overhead when specifying a Name or x:Name attribute for WPF elements

Is there any overhead associated with simply specifying a Name or x开发者_JAVA技巧:Name attribute on a WPF element? I had trouble locating any information regarding this, so maybe the answer is no, but I would like to get some feedback from those more familiar with the inner workings of WPF.

I guess I'm wondering for elements that have a Name if the WPF rendering engine does any special or extra work to prepare for the accessibility of that element from code, etc.


All it does is create a reference to the element in the generated code to make it easier for you to reference it in your code behind. Example: (from generated code file MainWindow.g.cs, which would be under the hidden "obj" folder of your project)

    #line 6 "..\..\..\MainWindow.xaml"
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
    internal System.Windows.Controls.Button button1;

    ...

    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
        switch (connectionId)
        {
        case 1:
        this.button1 = ((System.Windows.Controls.Button)(target));
        return;
        }
        this._contentLoaded = true;
    }

If you didn't give it a Name, the "((System.Windows.Controls.Button)(target))" would still be created at runtime, you just wouldn't have the "this.button1" variable pointing to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜