In my WPF app, all I have in my code-behind is the following:
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
Can I completely remove the code-behind file from my project or does it have to stay there? My background is in web app development and I’m kind of relating this to a code-behind file with an empty Page_Load() method, which I would typically remove.
You can remove it, if you use one of these techniques:
1: Remove the
x:classdeclaration from the top of your XAML file, and find a different way to instantiate the object (e.g., you can’t usenew MainWindow(), but you can useXamlReader.Loadand cast the result to a Window).or
2: Use this approach:
This moves the InitializeComponent call into your XAML, so you can delete the codebehind file.