0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NET Framework WPFのサンプル2

Last updated at Posted at 2024-11-16

環境

Windows 11 Pro (64bit)

dev.bat
@echo off
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
prompt $e[33m$p$g$e[m
cmd
wpf.rsp
# csc @wpf.rsp foo.cs
/r:System.Xaml.dll
/r:WPF\PresentationCore.dll
/r:WPF\PresentationFramework.dll
/r:WPF\WindowsBase.dll

計数器

元ネタはこちらです。圧倒的感謝……!

XAMLを使わない

fig1.cs
// csc /t:winexe @wpf.rsp fig1.cs
using System;
using System.Windows;
using System.Windows.Controls;

class MyWindow : Window {
	public MyWindow() {
		var panel = new DockPanel();
		var label = new Label();
		var button = new Button();

		label.Content = 0;
		button.Content = "Click!";
		button.Click += (sender, e) => {
			label.Content = (int)label.Content + 1;
		};
		DockPanel.SetDock(button, Dock.Bottom);

		panel.Children.Add(button);
		panel.Children.Add(label);

		Title = "test";
		Width = 240;
		Height = 100;
		Content = panel;
	}
}

class Program {
	[STAThread]
	static void Main() {
		var wnd = new MyWindow();
		wnd.ShowDialog();
	}
}

image.png

XAMLを使う

fig2.cs
// csc /t:winexe @wpf.rsp fig2.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

public class MyWindow : Window {
	Label label;

	void OnLoad(object sender, RoutedEventArgs e) {
		label = (Label)FindName("label");
		label.Content = 0;
	}

	void button_Click(object sender, RoutedEventArgs e) {
		label.Content = (int)label.Content + 1;
	}
}

class Program {
	[STAThread]
	static void Main() {
		var xaml = @"
<foo:MyWindow
 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
 xmlns:foo='clr-namespace:;assembly=fig2'
 Width='240' Height='100' Title='test' Loaded='OnLoad'>
	<DockPanel>
		<Button DockPanel.Dock='Bottom'
		 Click='button_Click'>Click!</Button>
		<Label Name='label' />
	</DockPanel>
</foo:MyWindow>
";
		try {
			var wnd = (MyWindow)XamlReader.Parse(xaml);
			wnd.ShowDialog();
		} catch (Exception e) {
			MessageBox.Show(e.ToString());
		}
	}
}

時計

XAMLを使わない

fig3.cs
// csc /t:winexe @wpf.rsp fig3.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

class MyWindow : Window {
	public MyWindow() {
		var clock = new TextBlock();
		clock.FontSize = 30;

		var timer = new DispatcherTimer();
		timer.Tick += (sender, e) => {
			var dt = DateTime.Now;
			clock.Text = dt.ToString();
		};
		timer.Interval = TimeSpan.FromSeconds(1);
		timer.Start();

		Title = "Clock";
		Width = 320;
		Height = 100;
		Content = clock;
	}
}

class Program {
	[STAThread]
	static void Main() {
		var wnd = new MyWindow();
		wnd.ShowDialog();
	}
}

image.png

XAMLを使う

fig4.cs
// csc /t:winexe @wpf.rsp fig4.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Threading;

public class MyWindow : Window {
	TextBlock clock;
	DispatcherTimer timer;

	void OnLoad(object sender, RoutedEventArgs e) {
		clock = (TextBlock)FindName("clock");
		clock.FontSize = 30;

		timer = new DispatcherTimer();
		timer.Tick += timer_Tick;
		timer.Interval = TimeSpan.FromSeconds(1);
		timer.Start();
	}

	void timer_Tick(object sender, EventArgs e) {
		var dt = DateTime.Now;
		clock.Text = dt.ToString();
	}
}

class Program {
	[STAThread]
	static void Main() {
		var xaml = @"
<foo:MyWindow
 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
 xmlns:foo='clr-namespace:;assembly=fig4'
 Width='320' Height='100' Title='Clock' Loaded='OnLoad'>
	<TextBlock Name='clock' />
</foo:MyWindow>
";
		try {
			var wnd = (MyWindow)XamlReader.Parse(xaml);
			wnd.ShowDialog();
		} catch (Exception e) {
			MessageBox.Show(e.ToString());
		}
	}
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?