「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
XAMLの書き方(1)の続き。
WPF / Silverlightは、Windows Formsとは全く異なるUIサブシステムであるが、
類似のビルティング ブロック クラスが使用されているため、よく似たコードでプログラムを記述することができる。
WPF / SilverlightにはWindows フォームと同様に Applicationオブジェクトが存在する。
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml"> <Application.Resources></Application.Resources> </Application>
Applicationオブジェクトには様々なイベント処理を実装することができる。
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml"
Startup="Application_Startup" Exit="Application_Exit"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application.Resources></Application.Resources>
</Application>private void Application_Startup(object sender, StartupEventArgs e) {
System.Diagnostics.Debug.WriteLine("開始処理");
}
private void Application_DispatcherUnhandledException(
object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) {
System.Diagnostics.Debug.WriteLine("エラー処理");
}
private void Application_Exit(object sender, ExitEventArgs e) {
System.Diagnostics.Debug.WriteLine("終了処理");
}private void Application_Startup(object sender, StartupEventArgs e) {
Application.Current.Properties["Key"] = "Value1";
}<Window x:Class="WpfApplication1.Window1" ・・・ Loaded="Window_Loaded"> <Grid> <Button Click="Button_Click">ボタン</Button> </Grid>
private void Window_Loaded(object sender, RoutedEventArgs e) {
MessageBox.Show((string)Application.Current.Properties["Key"]);
Application.Current.Properties["Key"] = "Value2";
}
private void Button_Click(object sender, RoutedEventArgs e) {
Window2 w2 = new Window2();
w2.Show();
}private void Window_Loaded(object sender, RoutedEventArgs e) {
MessageBox.Show((string)Application.Current.Properties["Key"]);
}WPFではWindows Formsと同様にWindow 画面を、以下のように起動できる(「XBAP」を除く)。
Window2 win2 = new Window2(); win2.Show();
Window2 win2 = new Window2(); win2.Owner = this; win2.ShowDialog();
Windowには、以下のようなプロパティを設定できる。
<NavigationWindow x:Class="WpfApplication1.NavWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="NavWindow" Height="300" Width="300"> </NavigationWindow>
public partial class NavWindow : NavigationWindow {
public NavWindow() {
InitializeComponent();
}
}WPFでNavigationWindow?画面を起動する方法について説明する。
NavWindow normalWindow = new NavWindow(); normalWindow.Show();
NavWindow dialogWindow = new NavWindow(); dialogWindow.Owner = this; bool returnValue = dialogWindow.ShowDialog() ?? false;
<NavigationWindow x:Class="WpfApplication1.NavWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NavWindow" Height="300" Width="300" Loaded="NavigationWindow_Loaded">
</NavigationWindow>public partial class NavWindow : NavigationWindow {
public NavWindow() {
InitializeComponent();
}
private void NavigationWindow_Loaded(object sender, RoutedEventArgs e) {
// Page1 を表示します。
this.Navigate(new Page1());
}
}<NavigationWindow x:Class="WpfApplication1.NavWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="NavWindow" Height="300" Width="300" Source="Page1.xaml"> </NavigationWindow>
<StackPanel Orientation="Vertical"> <Frame Source="Page2.xaml" Navigated="Frame1_Navigated"/> <Frame Source="Page3.xaml" Navigated="Frame2_Navigated"/> </StackPanel>
「XBAP」、「Silverlight」では、WindowNavigation? / Windowが使用出来ないため、以下の方法で、直接Page画面をロードする。
<Application x:Class="WpfBrowserApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Page1.xaml">
<Application.Resources>
</Application.Resources>
</Application><Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication1.App">
<Application.Resources>
</Application.Resources>
</Application>public App(){
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e) {
// this.RootVisual = new MainPage(); デフォルトは、ユーザコントロール
this.RootVisual = new Page1();
}なお、「Silverlight」では、
デフォルトの初期画面(Application.RootVisual?)が、ユーザ コントロールとなっているため、
通常、ユーザ コントロールにFrameを追加し、Frameでページ遷移させる方法を取る。
WPF / SilverlightにおけるPage画面の画面遷移方法について説明する。
<HyperlinkButton NavigateUri="/Page2.xaml">次のページへ移動します。</HyperlinkButton> <HyperlinkButton NavigateUri="/Page1.xaml">前のページへ移動します。</HyperlinkButton>
this.NavigationService.Navigate(new Page2());
this.NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));this.NavigationService.Navigate(
new Uri("pack://application:,,,/Page2.xaml", UriKind.Absolute));this.NavigationService.GoForward();
this.NavigationService.GoBack();
this.NavigationService.RemoveBackEntry();
private void NavigationWindow_Navigated(object sender, NavigationEventArgs e) {
this.NavigationService.RemoveBackEntry();
}private void Frame1_Navigated(object sender, NavigationEventArgs e) {
if (NavigationService !=null) {
if (NavigationService.CanGoBack) {
this.NavigationService.RemoveBackEntry();
}
}
}
private void Frame2_Navigated(object sender, NavigationEventArgs e) {
if (NavigationService != null) {
if (NavigationService.CanGoBack) {
this.NavigationService.RemoveBackEntry();
}
}
}<Page x:Class="WpfApplication1.Page1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Page1" ShowsNavigationUI="false">
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1" ShowsNavigationUI="false"
Loaded="Page_Loaded" KeyDown="Page_KeyDown">
<StackPanel>
<TextBlock>
<Hyperlink NavigateUri="Page2.xaml">Page2</Hyperlink>
</TextBlock>
<TextBox Name="textBox1"/>
</StackPanel>
</Page>public partial class Page1 : Page {
public Page1() {
InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e) {
this.textBox1.Focus();
}
private void Page_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Back) e.Handled = true;
}
}NavigationWindow? / Pageのその他のトピック
ChildWindow1 childWindow = new ChildWindow1(); childWindow.Show();
下記のMessageBox?クラスの名前空間は、Windows FormsではなくWPFのものだが、
このMessageBox?クラスのクラス階層を確認するとWPFのアーキテクチャに基づいていないことが確認できる。
Tags: :.NET開発, :UIサブシステム, :WPF/Silverlight, XAML