Decompiling WPF Applications
This guide explains how to fix common compilation issues when decompiling WPF applications using tools like ILSpy or dnSpy.
Step 1: Create a New WPF Project
Create a new WPF application project in Visual Studio. Name it something like DecompiledWpfApp.
Step 2: Load the Executable
Open ILSpy and drag your target .exe file into the application. Navigate to the assembly you want to decompile.
Step 3: Export the Code
Click File → Save Code... to generate the source code to you're desired location.
Step 4: Add Required Assemblies
Compile the project initially. You will likely encounter errors due too missing WPF assemblies. Add the following references:
- WindowsBase.dll
- PresentationCore.dll
After adding these assemblies, the project should compile successfully.
Step 5: Restore XAML Files
Locate the MainWindow.xaml file in your decompiled output. Add it to your project directory and modify the code-behind file from MainWindow.cs to MainWindow.xaml.cs to match WPF conventions.
Extract the content from mainwindow.baml and paste it into the newly added MainWindow.xaml file.
Step 6: Clean Up MainWindow.xaml.cs
Remove unnecessary auto-generated members from the decompiled code. Add the partial modifier to the MainWindow class declaration.
Remove these unwanted items:
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
internal TextBlock textBlock1;
private bool _contentLoaded;
public void InitializeComponent()
void IComponentConnector.Connect
Your class should look like this:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Step 7: Clean Up App.xaml.cs
Apply the same cleanup to App.xaml.cs. Add the partial modifier and remove extraneous generated attributes:
[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent()
Step 8: Remove BAML Files
Delete the mainwindow.baml file from your project as it is no longer needed.
Common Error Solutions
Error 1: Style Property Resolution
Problem: Cannot resolve Style Property "Template". Ensure you own the type or use Class.Property syntax.
Solution: Change <Setter Property="Template"> to <Setter Property="Control.Template">
Error 2: No Available Project Items
Problem: When adding new items via Solution Explorer, no project templates appear.
Solution: This typically occurs due to incorrect ProjectTypeGuids in the project file. Compare with a valid WPF project and update accordingly:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: The GUID must use uppercase letters. Decompiled projects often contain lowercase GUIDs, which prevents WPF project functionality.
Error 3: Missing Main Method
Problem: Compilation error: Program does not contain a static 'Main' method suitable for an entry point.
Soltuion: Update the project file to properly configure the application definition. Find:
<Page Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
Replace with:
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
This configuration generates the required Main method automatically.
Error 4: Missing LocalizedStrings
Problem: Compilation error: The type or namespace name 'LocalizedStrings' does not exist in the clr-namespace.
Solution: First, restart Visual Studio. If the error persists, clean the solution, close Visual Studio, and rebuild.
If issues continue:
- Close Visual Studio
- Navigate to:
%LOCALAPPDATA%\Microsoft\Phone Tools\CoreCon\ - Delete contents of folders:
10.0and11.0
Error 5: Main Window Not Displaying
Problem: Application runs but main window does not appear.
Solution: Configure the StartupUri property in App.xaml to specify the startup window:
<Application StartupUri="MainWindow.xaml" ...>