🔖
もうつまずかない。。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 コンパイル時にコード生成をトリガーします。
<Page>
の重複を解消
3. -
手順
-
推奨: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:Class
and code-behind - Incorrect Build Action on the
.xaml
file - Implicit vs. explicit
<Page>
duplication - Missing WPF declarations in the csproj
- Stale build cache
- Non-alphanumeric characters in paths or filenames
Solution
x:Class
and code-behind class
1. Match -
Action: Ensure
<Window x:Class="Sample.UI.Windows.SampleWindow">
matches the namespace and class inSampleWindow.xaml.cs
. -
Effect: The auto-generated
.g.cs
linking 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.
<Page>
definitions
3. Avoid duplicate -
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:Class
and 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