Search code examples
wpfcode-generationwindowprivate

How tell the code generator for XAML in WPF to declare my Window as "internal"?


How do I tell the code generator for XAML in WPF to declare my Window as "internal"?

By default when I run the VS2010 Create New Project, the and generated code is declared as "public". I'm using Autofac as my IoC, and I'm following the pattern where my ViewModel is set by property injection. I would like to declare my ViewModel as "private", but the compiler complains about inconsistent accessibility. I can change the change the "public" in MainWindow.xaml.cs to "internal", but how do I tell the code that generates the rest of the class to also declare the MainWindow as "internal"?

Code I currently have:

public partial class MainWindom : Window
{
    public ViewModel ViewModel { get; set; }
}

public class ViewModel
{
}

Code that I would like to have:

internal partial class MainWindom : Window
{
    public ViewModel ViewModel { get; set; }
}

internal class ViewModel
{
}

Or should I just give up not fight with the tools and just go with it?

Rick Sladkey correctly noted below, that I can't declare private at the namespace level, so I'm changing to "internal".


Solution

  • A class derived from Window that is defined in a top-level namespace (as all generated Window classes are) can never be declared private. Attempting to do so will generate a different error:

    error CS1527: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal

    so it is not possible to do what you are trying to do. Even if it were legal, there would be no way to access the class from other classes anyway.

    EDIT

    The window can be internal, read more: Making a XAML file internal in .Net