Domanda

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?

È stato utile?

Soluzione

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

Altri suggerimenti

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">
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top