🔖
もうつまずかない。。InitializeComponent が見つからないエラー撃退
【トラブル内容|Issue】
Sample\UI\Windows\SampleWindow.xaml.cs(40,13): error CS0103:
The name 'InitializeComponent' does not exist in the current context
自動生成されるはずの InitializeComponent() が見つからず発生するエラーです。
【原因|Cause】
- XAML の
x:Classとコードビハインドのクラス定義が一致していない -
.xamlファイルの Build Action が誤設定 - 暗黙的/明示的な
<Page>の重複登録 - csproj に WPF 用設定(UseWPF または FrameworkReference)がない
- 古いビルド成果物やキャッシュが残っている
- フォルダ名・ファイル名に英数字以外の文字が含まれている
【解決策|Solution】
1. クラス名・名前空間の整合性を確認
-
手順
XAML の<Window x:Class="Sample.UI.Windows.SampleWindow">が、コードビハインド側のnamespace Sample.UI.Windowsとclass SampleWindowに完全に合っているか確認します。 -
効果
XAML → 自動生成された*.g.csの関連付けが正しく行われ、InitializeComponent()の参照が解決されます。
2. Build Action を「Page」に設定
-
手順
Visual Studio のプロパティで、対象の.xamlファイルの Build Action を Page に切り替えます。 -
効果
MSBuild がファイルを WPF ページとして認識し、XAML コンパイル時にコード生成をトリガーします。
3. <Page> の重複を解消
-
手順
-
推奨:csproj 内の明示的な
<Page Include="…">行を削除し、SDK の暗黙的登録に任せる。 -
手動管理:
<EnableDefaultPageItems>false</EnableDefaultPageItems>を追加して暗黙登録をオフにし、必要な XAML のみ<Page>で個別に指定する。
-
推奨:csproj 内の明示的な
-
効果
重複登録がなくなるため、ビルド時の競合エラーを防げます。
4. WPF ビルドパイプラインを有効化
-
手順
csproj に以下のいずれかを追加します:<PropertyGroup> <UseWPF>true</UseWPF> </PropertyGroup>または
<ItemGroup> <FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" /> </ItemGroup> -
効果
XAML コンパイルや必要参照の自動設定が有効になり、WPF 固有のビルド処理が動作します。
5. クリーン&リビルドを実行
-
手順
Visual Studio の Clean Solution → Rebuild Solution を順に実行します。 -
効果
古い.g.csなどの中間ファイルやキャッシュが削除され、最新設定でビルドし直せます。
6. 英数字のみのパスに統一
-
手順
フォルダ名・ファイル名に含まれる空白や全角文字、特殊記号を削除し、英数字および-/_のみを使用します。 -
効果
自動生成クラスの名前解決エラーを防ぎ、XAML → CS 変換が正常に行われます。
【まとめ|Summary】
| 手順 | 効果 |
|---|---|
① XAML の x:Class とコードビハインドを一致させる |
InitializeComponent() の紐付けが正しく行われる |
| ② Build Action を Page にする | XAML コンパイル時にコード生成が走る |
③ <Page> の重複を解消 |
重複登録によるビルドエラーを防止 |
④ <UseWPF>/FrameworkReference を追加 |
WPF 向けビルド処理が有効化 |
| ⑤ Clean → Rebuild | 古い成果物を一掃し、最新設定で再ビルド |
| ⑥ 英数字パスに統一 | 自動生成エラーを防止 |
English Version
Issue
Sample\UI\Windows\SampleWindow.xaml.cs(40,13): error CS0103:
The name 'InitializeComponent' does not exist in the current context
The WPF-generated InitializeComponent() method is missing.
Cause
- Mismatch between XAML
x:Classand code-behind - Incorrect Build Action on the
.xamlfile - Implicit vs. explicit
<Page>duplication - Missing WPF declarations in the csproj
- Stale build cache
- Non-alphanumeric characters in paths or filenames
Solution
1. Match x:Class and code-behind class
-
Action: Ensure
<Window x:Class="Sample.UI.Windows.SampleWindow">matches the namespace and class inSampleWindow.xaml.cs. -
Effect: The auto-generated
.g.cslinking XAML to code-behind (includingInitializeComponent()) will resolve correctly.
2. Set Build Action to Page
-
Action: In Properties for
SampleWindow.xaml, select Build Action: Page. - Effect: MSBuild treats the file as a WPF page and triggers XAML compilation.
3. Avoid duplicate <Page> definitions
-
Action:
-
Recommended: Remove explicit
<Page Include="...">entries from the csproj. -
Manual management: Set
<EnableDefaultPageItems>false</EnableDefaultPageItems>and explicitly list each XAML.
-
Recommended: Remove explicit
-
Effect: Eliminates duplicate registration; only one
<Page>item per XAML.
4. Enable WPF in csproj
-
Action: Add either
<UseWPF>true</UseWPF>or
<FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" /> -
Effect: Activates the WPF build pipeline (XAML compilation, required references).
5. Clean & Rebuild
- Action: Execute Clean Solution then Rebuild Solution in Visual Studio.
- Effect: Clears out old generated files and caches, ensuring a fresh build.
6. Use alphanumeric-only paths
- Action: Rename folders/files to remove spaces, full-width or special characters.
- Effect: Prevents invalid tokens in generated class/file names, ensuring successful code generation.
Summary
-
Align
x:Classand code-behind for proper code generation. - Build Action = Page to compile XAML.
-
Remove duplicate
<Page>or disable implicit items. -
Add
<UseWPF>/FrameworkReference to enable WPF. - Clean & Rebuild to purge stale artifacts.
- Use alphanumeric paths to avoid naming issues.
Following these steps will restore the auto-generated InitializeComponent() and resolve the CS0103 error.
Discussion