質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.33%
.NET MAUI

.NET MAUIは、「.NET Multi-platform App UI」の略。単一コードで複数のプラットフォームに対応するクロスプラットフォームフレームワークです。Xamarin.Formsの進化系とされており、XAMLやMVVM に加え、MVUもサポートされています。

C#

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

Q&A

解決済

1回答

226閲覧

[C#][.NET MAUI] デバック用のウィンドウを生成して表示したいが、コードがこんがらがってうまくいかない。

yukkuri_55

総合スコア271

.NET MAUI

.NET MAUIは、「.NET Multi-platform App UI」の略。単一コードで複数のプラットフォームに対応するクロスプラットフォームフレームワークです。Xamarin.Formsの進化系とされており、XAMLやMVVM に加え、MVUもサポートされています。

C#

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

1グッド

1クリップ

投稿2025/01/26 12:48

実現したいこと

.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についてはふれない)

TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーメッセージ

書いてある通りその時点では、Application.Currentnullです。
Application.Current プロパティ (Microsoft.Maui.Controls) | Microsoft Learn

Application.Currentが入った後ならどこでもいいんでしょうが、一番手軽なのはMainPageのコンストラクタあたりでしょうか?

cs

1public partial class MainPage : ContentPage 2{ 3 public MainPage() 4 { 5 InitializeComponent(); 6 7 var debugWindow = new DebugWindow(); 8 Window newWindow = new Window(debugWindow); 9 CommonData.SetDebugWindowInfo(debugWindow); 10 Application.Current.OpenWindow(newWindow); 11 } 12}

.NET MAUI 環境にて、メインウィンドウと、デバック用ウィンドウ、2つ生成して同時に表示したい。

そもそもデバッグ目的なら、Debug.WriteLine等で「出力」ウィンドウに表示すればよくないですか?
Debug.WriteLine メソッド (System.Diagnostics) | Microsoft Learn

アプリ内に出したいなら下手に別ページにするよりは、別ウィンドウのほうが扱いは楽そうですが...


cs

1public partial class DebugWindow : ContentPage

本当はページなのにウィンドウを名乗るのは、かなり気持ちが悪いです^^;

「.NET MAUI Window (XAML)」に変更すれば、名前通りになりますね^^

xml:DebugWindow.xaml

1<?xml version="1.0" encoding="utf-8" ?> 2<Window 3 x:Class="Q6ezm2rj0la9dh8.DebugWindow" 4 xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 5 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 6 Title="DebugWindow"> 7 <ContentPage> 8 <ScrollView> 9 <Label x:Name="Label_message" /> 10 </ScrollView> 11 </ContentPage> 12</Window>

cs:DebugWindow.xaml.cs

1namespace Q6ezm2rj0la9dh8; 2 3public partial class DebugWindow : Window 4{ 5 public DebugWindow() 6 { 7 InitializeComponent(); 8 9 var w = Application.Current!.Windows[0]; 10 X = w.X + w.Width; 11 Y = w.Y; 12 Width = 300; 13 Height = 500; 14 } 15 16 public void AddMessage(string message) 17 { 18 Label_message.Text += message; 19 } 20}

cs:MainPage.xaml.cs

1namespace Q6ezm2rj0la9dh8; 2 3public partial class MainPage : ContentPage 4{ 5 public MainPage() 6 { 7 InitializeComponent(); 8 9 var debugWindow = new DebugWindow(); 10 CommonData.SetDebugWindowInfo(debugWindow); 11 Application.Current!.OpenWindow(debugWindow); 12 } 13}

投稿2025/01/26 14:24

編集2025/01/26 15:19
TN8001

総合スコア9937

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

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

yukkuri_55

2025/01/26 20:07

public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); var debugWindow = new DebugWindow(); Window newWindow = new Window(debugWindow); CommonData.SetDebugWindowInfo(debugWindow); Application.Current.OpenWindow(newWindow); } } おっしゃるおおり、上のようにコードを変更すると、デバック用ウィンドウが出ました。 解決しました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.33%

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

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

質問する

関連した質問