🔖

もうつまずかない。。InitializeComponent が見つからないエラー撃退

に公開

【トラブル内容|Issue】

Sample\UI\Windows\SampleWindow.xaml.cs(40,13): error CS0103:
The name 'InitializeComponent' does not exist in the current context

自動生成されるはずの InitializeComponent() が見つからず発生するエラーです。


【原因|Cause】

  1. XAML の x:Class とコードビハインドのクラス定義が一致していない
  2. .xaml ファイルの Build Action が誤設定
  3. 暗黙的/明示的な <Page> の重複登録
  4. csproj に WPF 用設定(UseWPF または FrameworkReference)がない
  5. 古いビルド成果物やキャッシュが残っている
  6. フォルダ名・ファイル名に英数字以外の文字が含まれている

【解決策|Solution】

1. クラス名・名前空間の整合性を確認

  • 手順
    XAML の <Window x:Class="Sample.UI.Windows.SampleWindow"> が、コードビハインド側の namespace Sample.UI.Windowsclass SampleWindow に完全に合っているか確認します。
  • 効果
    XAML → 自動生成された *.g.cs の関連付けが正しく行われ、InitializeComponent() の参照が解決されます。

2. Build Action を「Page」に設定

  • 手順
    Visual Studio のプロパティで、対象の .xaml ファイルの Build ActionPage に切り替えます。
  • 効果
    MSBuild がファイルを WPF ページとして認識し、XAML コンパイル時にコード生成をトリガーします。

3. <Page> の重複を解消

  • 手順

    • 推奨:csproj 内の明示的な <Page Include="…"> 行を削除し、SDK の暗黙的登録に任せる。
    • 手動管理<EnableDefaultPageItems>false</EnableDefaultPageItems> を追加して暗黙登録をオフにし、必要な XAML のみ <Page> で個別に指定する。
  • 効果
    重複登録がなくなるため、ビルド時の競合エラーを防げます。

4. WPF ビルドパイプラインを有効化

  • 手順
    csproj に以下のいずれかを追加します:

    <PropertyGroup>
      <UseWPF>true</UseWPF>
    </PropertyGroup>
    

    または

    <ItemGroup>
      <FrameworkReference Include="Microsoft.WindowsDesktop.App.WPF" />
    </ItemGroup>
    
  • 効果
    XAML コンパイルや必要参照の自動設定が有効になり、WPF 固有のビルド処理が動作します。

5. クリーン&リビルドを実行

  • 手順
    Visual Studio の Clean SolutionRebuild 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

  1. Mismatch between XAML x:Class and code-behind
  2. Incorrect Build Action on the .xaml file
  3. Implicit vs. explicit <Page> duplication
  4. Missing WPF declarations in the csproj
  5. Stale build cache
  6. 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 in SampleWindow.xaml.cs.
  • Effect: The auto-generated .g.cs linking XAML to code-behind (including InitializeComponent()) 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.
  • 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

  1. Align x:Class and code-behind for proper code generation.
  2. Build Action = Page to compile XAML.
  3. Remove duplicate <Page> or disable implicit items.
  4. Add <UseWPF>/FrameworkReference to enable WPF.
  5. Clean & Rebuild to purge stale artifacts.
  6. Use alphanumeric paths to avoid naming issues.

Following these steps will restore the auto-generated InitializeComponent() and resolve the CS0103 error.

Discussion