**📂WindowShowExample
┣ 📂GUI
┃ ┣ 📜MainWindow.xaml # 画面の構成 (XAMLファイル)
┃ ┣ 📜MainWindow.cs # 画面のコードビハインド (C#ファイル)
┗ 📜App.cs # アプリケーションのエントリーポイント (C#ファイル)**
<Window
xmlns="<http://schemas.microsoft.com/winfx/2006/xaml/presentation>"
xmlns:x="<http://schemas.microsoft.com/winfx/2006/xaml>"
Title="Window.Show Sample" Height="200" Width="300">
<Grid>
<TextBlock Text="Hello, this is a Window.Show example!" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16"/>
</Grid>
</Window>
using System.Windows; // WPFのWindowクラスを使用するための名前空間
using System.Windows.Markup; // XAMLの読み込みに必要な名前空間
using System.IO; // ファイルの操作に必要な名前空間
namespace WindowShowExample
{
public class MainWindow : Window
{
public MainWindow()
{
// XAMLファイルのパス
string xamlPath = "GUI/MainWindow.xaml";
// XAMLファイルを開いてロードする
using (FileStream fs = new FileStream(xamlPath, FileMode.Open, FileAccess.Read))
{
// XAMLファイルからWindowオブジェクトを生成する
var window = (Window)XamlReader.Load(fs);
// ロードしたWindowのプロパティを現在のウィンドウに適用する
this.Content = window.Content;
this.Title = window.Title;
this.Height = window.Height;
this.Width = window.Width;
}
}
}
}
InitializeComponent
メソッドの代わりに、XamlReader
**を使用してXAMLファイルをロードし、そのプロパティを設定しています。using System; // 基本的なシステム機能に必要な名前空間
using System.Windows; // WPFアプリケーションを作成するための名前空間
namespace WindowShowExample
{
public class App : Application
{
[STAThread] // シングルスレッドアパートメントモデルを使用することを示す属性
public static void Main()
{
// 新しいアプリケーションインスタンスを作成
App app = new App();
// 新しいメインウィンドウインスタンスを作成
MainWindow mainWindow = new MainWindow();
// メインウィンドウを表示
mainWindow.Show();
// アプリケーションを実行
app.Run();
}
}
}
Main
**メソッドでは、新しいアプリケーションインスタンスとメインウィンドウインスタンスを作成し、メインウィンドウを表示してアプリケーションを実行します。C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\csc.exe /reference:"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\WindowsBase.dll","C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\PresentationCore.dll","C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\v3.0\\PresentationFramework.dll" /target:winexe /out:WindowShowExample.exe GUI\\MainWindow.cs App.cs
Microsoft (R) Visual C# Compiler version 4.8.9232.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see <http://go.microsoft.com/fwlink/?LinkID=533240>
warning CS1685: 定義済みの型 'System.Collections.Specialized.INotifyCollectionChanged' は、グローバル
エイリアスの複数のアセンブリ内で定義されています。'c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\System.dll'
からの定義を使用してください。
/nowarn:1685
をつけることで警告を非表示にする。