Why InitializeComponent is public
Asked Answered
D

2

7

Public interface of my WPF user control contains autogenerated InitializeComponent method (which is contained in a partial class). It was a surprise for me as I expected such an internal stuff to be private.

Is there any way to remove InitializeComponent from user control public interface?

Dwarf answered 10/5, 2012 at 8:11 Comment(5)
Without a call to initializeComponent XAML wont get parsedRickie
@Sharun, yep, but it should not be called outside the controlDwarf
@Sharun: It is called inside the constructor of the control, isn't it? So it doesn't have to be public...Griselgriselda
You are right. Misunderstood the question. I guess you have to go into each file and change it.Rickie
@Idsa I have edited my answer below and added an SO QA discussion link to it for similar question which would help you.Personate
P
6

InitializeComponent is a method defined on the interface System.Windows.Markup.IComponentConnector and is used for loading the compiled page of a component.

See MSDN excerpt below from this link which has more info:

IComponentConnector is used internally by Baml2006Reader.

Implementations of InitializeComponent are widely observable as part of the infrastructure provided by frameworks or technologies that use XAML combined with application and programming models. For example, whenever you look at the generated classes for XAML root elements in WPF pages and applications, you will see InitializeComponent defined in the output. That method also exists in the compiled assembly and plays a role in the WPF application model of loading the XAML UI content at XAML parse time (and I suppose hence InitializeComponent has to be in an interface and be public so that other outside WPF related assemblies can make use of it).

To explain this further, go to the definition of InitializeComponent() method in your (say): Window1.g.cs class of say: WPFProject project, and change its access from public to private

(keep the .g.cs file open in your project otherwise the build process overrides this file, and you won't be able to see the error)

Now, when you compile your WPF project, it throws a compile error as below:

Error 22 'WPFProject.Window1' does not implement interface member 'System.Windows.Markup.IComponentConnector.InitializeComponent()'. 'WPFProject.Window1.InitializeComponent()' cannot implement an interface member because it is not public.

Additionally, InitializeComponent() is marked with the [System.Diagnostics.DebuggerNonUserCodeAttribute()] attribute so you can't step into this method while debugging.

There is another SO QA discussion, which would help you to explain more in detail

Personate answered 10/5, 2012 at 11:2 Comment(1)
More to the point, external code can always make the call by simply casting the object to the interface type. Explicitly implementing InitializeComponent() so it is private thus doesn't solve any problem.Sankhya
J
0

Would you feel better if you made your control internal?

<UserControl 
     x:Class="PBbase.Client.Navigation.UserControl1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:ClassModifier="internal">
Jeminah answered 10/5, 2012 at 9:18 Comment(5)
But I need to keep control public, I just want to hide InitializeComponentDwarf
@Idsa Hide it? For what reason?Appeasement
@Erode, for what reason should I keep it public?Dwarf
@Idsa I was thinking you might want the control over logic prior to loading and parsing BAML. It can't really be abused, it's not dangerous. It's all rather innocuous to me which is why I asked why it was important for you to hide it.Appeasement
@Erode, I don't want to change any logic, I just don't want my control users to see this methodDwarf

© 2022 - 2024 — McMap. All rights reserved.