実現したいこと
.NET MAUI 環境にて、メインウィンドウと、デバック用ウィンドウ、2つ生成して同時に表示したい。
発生している問題・分からないこと
Window型やDebugWindowクラスなどが、こんがらがって、わけがわからなくなりました。
エラーメッセージ
error
1上の画像の通りです。
該当のソースコード
cs
1using Microsoft.Extensions.Logging; 2 3namespace MauiApp02_2025_01_06 4{ 5 public static class MauiProgram 6 { 7 public static MauiApp CreateMauiApp() 8 { 9 var builder = MauiApp.CreateBuilder(); 10 builder 11 .UseMauiApp<App>() 12 .ConfigureFonts(fonts => 13 { 14 fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); 15 fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold"); 16 }); 17 18#if DEBUG 19 builder.Logging.AddDebug(); 20#endif 21 22 23 // 新しいウィンドウを作成 24 var debugWindow = new DebugWindow(); // DebugWindow のインスタンスを作成 25 Window newWindow = new Window(debugWindow); // 新しいウィンドウを作成 26 CommonData.SetDebugWindowInfo(debugWindow); // DebugWindow のインスタンスを CommonData に設定 27 Application.Current.OpenWindow(newWindow); // 新しいウィンドウを開く 28 29 30 return builder.Build(); 31 } 32 } 33} 34
cs
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using System.Data.SQLite; 7 8namespace MauiApp02_2025_01_06 9{ 10 public static class CommonData 11 { 12 public static DebugWindow? m_debugWindow { get; set; } 13 public static int m_i第4章_第1節_正解した数 { get; set; } 14 // データベースへのパス 15 public static string? m_szdbPath { get; set; } 16 // SQLite接続文字列を作成 17 public static string? m_szconnectionString; 18 19 public static void SetDebugWindowInfo(DebugWindow debugWindow) 20 { 21 m_debugWindow = debugWindow; 22 } 23 public static void AddDebugMessage(string message) 24 { 25 m_debugWindow.AddMessage(message); 26 } 27 public static void 初期化() 28 { 29 m_szdbPath = "C:\\Users\\lalal\\OneDrive\\ドキュメント\\2025\\4択問題データベース関連\\sample"; 30 m_szconnectionString = $"Data Source={CommonData.m_szdbPath};Version=3;"; 31 } 32 33 public static void 初期化_第4章_第1節() 34 { 35 // レコードクラスのインスタンスを格納するリスト 36 List<レコード> records = new List<レコード>(); 37 38 // SQLiteConnectionオブジェクトを作成 39 using (SQLiteConnection connection = new SQLiteConnection(CommonData.m_szconnectionString)) 40 { 41 try 42 { 43 // データベースに接続 44 connection.Open(); 45 CommonData.AddDebugMessage("SQLiteデータベースに接続しました。(OnLoadBtnClicked)\n"); 46 // データを取得するSQLクエリ 47 string selectQuery = "SELECT ID, 名前, 正解数, 記録時間 FROM 記録"; 48 using (SQLiteCommand command = new SQLiteCommand(selectQuery, connection)) 49 { 50 using (SQLiteDataReader reader = command.ExecuteReader()) 51 { 52 while (reader.Read()) 53 { 54 // 各列の値を取得し、レコードクラスのインスタンスを作成 55 レコード record = new レコード 56 { 57 ID = reader.GetInt32(0), // ID 58 名前 = reader.GetString(1), // 名前 59 正解数 = reader.GetInt32(2), // 正解数 60 記録時間 = reader.GetString(3) // 記録時間 61 }; 62 63 // リストにRecordインスタンスを追加 64 records.Add(record); 65 } 66 } 67 } 68 69 // 読み込んだデータを表示 70 foreach (var record in records) 71 { 72 CommonData.AddDebugMessage($"ID: {record.ID}, 名前: {record.名前}, 正解数: {record.正解数}, 記録時間: {record.記録時間}\n"); 73 } 74 75 // 読み込んだレコード全部の内、最大の正解数を求める。 76 int maxScore = レコード.最大正解数(records); 77 CommonData.AddDebugMessage($"最大の正解数:{maxScore}\n"); 78 } 79 catch (SQLiteException ex) 80 { 81 // エラーメッセージを表示 82 CommonData.AddDebugMessage($"エラー: {ex.Message}\n"); 83 } 84 finally 85 { 86 // 接続を閉じる 87 connection.Close(); 88 CommonData.AddDebugMessage("接続を閉じました。\n"); 89 } 90 } 91 92 } 93 } 94 95} 96
xaml
1<?xml version="1.0" encoding="utf-8" ?> 2<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 3 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 4 x:Class="MauiApp02_2025_01_06.DebugWindow" 5 Title="DebugWindow"> 6 <VerticalStackLayout> 7 <Label 8 Text="DebugWindow" 9 x:Name="Label_message" 10 VerticalOptions="Center" 11 HorizontalOptions="Center" /> 12 </VerticalStackLayout> 13</ContentPage> 14
cs
1namespace MauiApp02_2025_01_06; 2 3//public partial class DebugWindow : ContentPage 4public partial class DebugWindow : ContentPage 5{ 6 public DebugWindow() 7 { 8 InitializeComponent(); 9 } 10 11 public void AddMessage( string message ) 12 { 13 Label_message.Text += message; 14 } 15} 16
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
AIに聞いてみたが、文字数オーバーで聞けなかった。
省略したりして、文字数オーバーをなくしたが、解決にはいたらなかった。
AIにソースコードを作ってもらった(どんな問題が発生してますか?のところの図)が
実行時にエラーが出る。
補足
OS:Windows 11 pro
言語: C#
FW: .NET MAUI
IDE: Visual Studio 2022 community
DB: SQLite (今回はDBについてはふれない)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2025/01/26 20:07