質問するログイン新規登録
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

Q&A

0回答

295閲覧

Visualstudio2022のWPFプロジェクトで、g.csが原因のエラーが出る。

kahito

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

0グッド

0クリップ

投稿2025/06/12 10:58

0

0

実現したいこと

ビルドを成功させたい。

発生している問題・分からないこと

WPFアプリの開発をおこなっているときに、ビルドしようとしたら、このようなエラーが起きて、ビルドに失敗した。ソースコードは一部分。

エラーメッセージ

error

11>C:\Users\tazu0\source\repos\WpfApp7\.vs\WpfApp7\obj\WpfApp7\Debug\net8.0-windows\App.g.cs(52,21,52,40): warning CS0108: 'GeneratedApp.InitializeComponent()' は継承されたメンバー 'App.InitializeComponent()' を非表示にします。非表示にする場合は、キーワード new を使用してください。 21>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml.cs(686,26,686,36): error CS8646: 'IComponentConnector.Connect(int, object)' が複数回、明示的に実装されています。 31>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml(206,47,206,59): error CS0102: 型 'MainWindow' は既に 'SupportPanel' の定義を含んでいます 41>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml(207,49,207,57): error CS0102: 型 'MainWindow' は既に 'Support1' の定義を含んでいます 51>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml(214,49,214,56): error CS0102: 型 'MainWindow' は既に 'MenuBtn' の定義を含んでいます 61>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml(219,54,219,63): error CS0102: 型 'MainWindow' は既に 'MenuItems' の定義を含んでいます 71>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml(240,47,240,55): error CS0102: 型 'MainWindow' は既に 'MainGuid' の定義を含んでいます 81>C:\Users\tazu0\source\repos\WpfApp7\.vs\WpfApp7\obj\WpfApp7\Debug\net8.0-windows\MainWindo.g.cs(99,22,99,36): error CS0102: 型 'MainWindow' は既に '_contentLoaded' の定義を含んでいます 91>C:\Users\tazu0\source\repos\WpfApp7\.vs\WpfApp7\obj\WpfApp7\Debug\net8.0-windows\MainWindo.g.cs(106,21,106,40): error CS0111: 型 'MainWindow' は、'InitializeComponent' と呼ばれるメンバーを同じパラメーターの型で既に定義しています 101>C:\Users\tazu0\source\repos\WpfApp7\.vs\WpfApp7\obj\WpfApp7\Debug\net8.0-windows\MainWindo.g.cs(126,56,126,63): error CS0111: 型 'MainWindow' は、'System.Windows.Markup.IComponentConnector.Connect' と呼ばれるメンバーを同じパラメーターの型で既に定義しています 111>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml.cs(9474,81,9474,85): warning CS8625: null リテラルを null 非許容参照型に変換できません。 121>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml.cs(9525,84,9525,88): warning CS8625: null リテラルを null 非許容参照型に変換できません。 131>C:\Users\tazu0\source\repos\WpfApp7\WpfApp7\MainWindo.xaml.cs(9590,86,9590,90): warning CS8625: null リテラルを null 非許容参照型に変換できません。 141>プロジェクト "WpfApp7_i2zrwlgq_wpftmp.csproj" のビルドが終了しました -- 失敗。

該当のソースコード

MainWindo.xaml.cs

1using System; 2using System.CodeDom.Compiler; 3using System.Collections; 4using System.Collections.Generic; 5using System.Collections.ObjectModel; 6using System.ComponentModel; 7using System.Configuration; 8using System.Diagnostics; 9using System.Globalization; 10using System.IO; 11using System.Linq; 12using System.Management; 13using System.Net; 14using System.Net.NetworkInformation; 15using System.Net.Security; 16using System.Reflection; 17using System.Resources; 18using System.Runtime.CompilerServices; 19using System.Runtime.InteropServices; 20using System.Runtime.Versioning; 21using System.Security.Cryptography.X509Certificates; 22using System.Text; 23using System.Text.RegularExpressions; 24using System.Threading; 25using System.Threading.Tasks; 26using System.Windows; 27using System.Windows.Controls; 28using System.Windows.Controls.Primitives; 29using System.Windows.Documents; 30using System.Windows.Input; 31using System.Windows.Markup; 32using System.Windows.Media; 33using System.Windows.Media.Animation; 34using System.Windows.Media.Imaging; 35using System.Windows.Shapes; 36using System.Windows.Threading; 37using AppleComponentSupport; 38using CommonServiceLocator; 39using Core.Downloader.Business; 40using Core.Http; 41using Core.Tracing; 42using Core.Tracing.Enum; 43using Core.Tracing.GA4; 44using Core.Tracing.GA4.Model; 45using IBackupLibrary.Decription; 46using iMobie.iDeviceConfig; 47using LogLib; 48using Microsoft.Win32; 49using Module.Base; 50using Module.Base.Model; 51using Module.Base.Utilities; 52using Module.Base.View.DialogWindow; 53using Module.Base.ViewModel; 54using Module.Base.ViewModel.DialogWindow; 55using Module.Base.ViewModel.Module; 56using Newtonsoft.Json; 57using Prism; 58using Prism.Commands; 59using Prism.Events; 60//using static Prism.Events.EventBase; 61using Prism.Ioc; 62using Prism.Modularity; 63using Prism.Mvvm; 64using Prism.Regions; 65using Prism.Services.Dialogs; 66using Prism.Unity; 67using Project.Shell.Controls; 68using Project.Shell.Model; 69using Project.Shell.Model.Activate; 70using Project.Shell.Model.Environment; 71using Project.Shell.Model.Update; 72using Project.Shell.Properties; 73using Project.Shell.Utilities; 74using Project.Shell.Utilities.NewActivate; 75using Project.Shell.Utilities.UpdateConfig; 76using Project.Shell.View; 77using Project.Shell.View.Window; 78using Project.Shell.View.Window.Activate; 79using Project.Shell.View.Window.Activity; 80using Project.Shell.View.Window.Device; 81using Project.Shell.ViewModel; 82using Project.Shell.ViewModel.Window; 83using Project.Shell.ViewModel.Window.Activate; 84using Project.Shell.ViewModel.Window.Activity; 85using Project.Shell.ViewModel.Window.Device; 86using Project.Structure.SEnum; 87using Project.Structure.SEvent; 88using Project.Structure.SEvent.Events; 89using Project.Structure.SEvent.Events.FunctionButton; 90using Project.Structure.SPrism; 91using Resource.Controls; 92using Resource.Utilities; 93using Service.iOSSupport; 94using Service.iOSSupport.DeviceBackup; 95using Service.iOSSupport.DeviceRestore; 96using Service.iOSSupport.Model; 97using Service.RG; 98using Service.RG.Config; 99using Service.RG.Model; 100using UI.Controls; 101using UI.Controls.Controls; 102using UI.Controls.Controls.IElement; 103using UI.Controls.Enum; 104using Utilities; 105 106public partial class MainWindow : CustomWindow, IComponentConnector 107{ 108 internal Grid SupportPanel; 109 110 internal Button Support1; 111 112 internal Button MenuBtn; 113 114 internal ContextMenu MenuItems; 115 116 internal Grid MainGuid; 117 118 private bool _contentLoaded; 119 120 121 public MainWindow() 122 { 123 InitializeComponent(); 124 EventTool.GetEvent<EventGetSupportPosition>().Subscribe(OnGetSupportPosition); 125 } 126 127 private void OnGetSupportPosition(Action<Func<Rect>> obj) 128 { 129 obj(delegate 130 { 131 Point point = SupportPanel.TranslatePoint(default(Point), this); 132 return (base.FlowDirection == FlowDirection.LeftToRight) ? new Rect(point.X, point.Y, SupportPanel.ActualWidth, SupportPanel.ActualHeight) : new Rect(base.ActualWidth - point.X, point.Y, SupportPanel.ActualWidth, SupportPanel.ActualHeight); 133 }); 134 } 135 private void CustomWindow_PreviewKeyDown(object sender, KeyEventArgs e) 136 { 137 ((PubSubEvent<KeyEventArgs>)(object)EventTool.GetEvent<EventKeyDown>()).Publish(e); 138 } 139 private void Menu_Click(object sender, RoutedEventArgs e) 140 { 141 MenuItems.PlacementTarget = MenuBtn; 142 MenuItems.Placement = PlacementMode.Custom; 143 ContextMenu menuItems = MenuItems; 144 menuItems.CustomPopupPlacementCallback = (CustomPopupPlacementCallback)Delegate.Remove(menuItems.CustomPopupPlacementCallback, new CustomPopupPlacementCallback(MenuItemCallBack)); 145 ContextMenu menuItems2 = MenuItems; 146 menuItems2.CustomPopupPlacementCallback = (CustomPopupPlacementCallback)Delegate.Combine(menuItems2.CustomPopupPlacementCallback, new CustomPopupPlacementCallback(MenuItemCallBack)); 147 MenuItems.MinWidth = 190.0; 148 MenuItems.IsOpen = false; 149 MenuItems.IsOpen = true; 150 } 151 152 private CustomPopupPlacement[] MenuItemCallBack(Size popupSize, Size targetSize, Point offset) 153 { 154 CustomPopupPlacement[] array = new CustomPopupPlacement[1]; 155 Point popupPoint = PlacementTool.GetPopupPoint(popupSize, targetSize, PlacementEnum.Bottom, default(Thickness), base.FlowDirection); 156 array[0] = new CustomPopupPlacement(popupPoint, PopupPrimaryAxis.None); 157 return array; 158 } 159 160 161 [DebuggerNonUserCode] 162 [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] 163 public void InitializeComponent() 164 { 165 if (!_contentLoaded) 166 { 167 _contentLoaded = true; 168 Uri resourceLocator = new Uri("/AnyUnlock - iPhone Password Unlocker;component/mainwindow.xaml", UriKind.Relative); 169 Application.LoadComponent(this, resourceLocator); 170 } 171 } 172 173 [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] 174 [DebuggerNonUserCode] 175 [EditorBrowsable(EditorBrowsableState.Never)] 176 void IComponentConnector.Connect(int connectionId, object target) 177 { 178 switch (connectionId) 179 { 180 default: 181 _contentLoaded = true; 182 break; 183 case 1: 184 ((MainWindow)target).PreviewKeyDown += CustomWindow_PreviewKeyDown; 185 break; 186 case 2: 187 SupportPanel = (Grid)target; 188 break; 189 case 3: 190 Support1 = (Button)target; 191 break; 192 case 4: 193 MenuBtn = (Button)target; 194 MenuBtn.Click += Menu_Click; 195 break; 196 case 5: 197 MenuItems = (ContextMenu)target; 198 break; 199 case 6: 200 MainGuid = (Grid)target; 201 break; 202 } 203 } 204}

試したこと・調べたこと

  • teratailやGoogle等で検索した
  • ソースコードを自分なりに変更した
  • 知人に聞いた
  • その他
上記の詳細・結果

このエラー文の一部をコピペして調べてみたが、あまり参考になる情報がなかった。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TN8001

2025/06/12 12:13

> Visualstudio2022のWPFプロジェクトで、g.csが原因のエラーが出る。 1行目はwarningで、errorは2行目以降ではないですか。 > WPFアプリの開発をおこなっているときに、ビルドしようとしたら、このようなエラーが起きて、ビルドに失敗した ビルドできていた時との差分はわかりませんか? > ソースコードは一部分。 InitializeComponent()の意図が分からないのですが、別のアセンブリからxamlだけ読み込もうとしていますか? 通常MainWindo.xamlがあれば、InitializeComponentは自動生成されます(まあMainWindo.g.csから持ってきたコードなんでしょうけど... それとかち合ってしまってエラーになっているような感じです。 やったことがないのでわかりませんが、MainWindo.xamlを消してMainWindo.csだけの状態(つまり単なるクラスを追加する)にすれば通るのかもしれません。 もちろんApp.xamlでのStartupUriは効かなくなりますので、自分でインスタンス化・表示が必要です。 [WPF定石 - スタートアップルーチン #C# - Qiita](https://qiita.com/chai0917/items/1d1ca54ca82ab2145094)
tamoto

2025/06/12 13:00

プロジェクトデフォルトが MainWindow であるところを MainWindo と誤字になっている点が気になりますね。 そこで根拠のないエスパーですが、MainWindow.xaml を誤ってリネームしたことで MainWindow クラスの partial 定義 (CodeGeneration) が2つ以上作られてしまったことでビルドエラーになっている可能性があるかもしれません。 一旦 Visual Studio を閉じて、ソリューションディレクトリにある .vs/ ディレクトリ、プロジェクトディレクトリにある bin/ obj/ ディレクトリをそれぞれ消してから再起動し再ビルドすると直るかもしれません。
kahito

2025/06/14 09:06

誤字を直し、obj/ディレクトリを消して再ビルドしましたが、ダメでした。
sheephuman

2025/06/24 02:31

最初から作り直した方が早いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問