Window、Page與Frame
- 一、Window
- 1.模態窗口與非模態窗口
- 2.Window類
- 3.示例
- 二、Page
- 1.概述
- 2.Page類
- 三、Frame
- 1.概述
- 2.Frame類
- 3.示例
- 四、ViewBox
- 1. 概述
- 2. 詳解
- 3. 示例
- 總結
一、Window
1.模態窗口與非模態窗口
2.Window類
屬性 | 說 明 |
---|---|
Title | 獲取或設置窗口的標題。 |
lcon | 設獲取或設置窗口的圖標。 |
WindowStartupLocation | 獲取或設置窗口首次顯示時的位置。 |
WindowState | 獲取或設置一個值,該值指示窗口是處于還原、最小化還是最大化狀態。 |
WindowStyle | 獲取或設置窗口的樣式 |
方法 | 說 明 |
Activate() | 將窗口激活并將其帶到前臺 |
Close() | 關閉窗口 |
ShowDialog() | 顯示窗口并等待窗口關閉后才返回(模態) |
Show() | 顯示窗口并返回(非模態) |
Hide() | 隱藏窗口 |
3.示例
private void buttonMT_Click(object sender, RoutedEventArgs e)
{Window1 window = new Window1();window.ShowDialog();
}private void buttonFMT_Click(object sender, RoutedEventArgs e)
{Window1 window = new Window1();//設置父窗口,父窗口關閉時,子窗口一起關閉window.Owner = this;// 計算模態窗口的位置,以顯示器左上角為起點,左,上,右,下double left = Left + (Width - window.Width) / 2;double top = Top + (Height - window.Height) / 2;// 設置模態窗口的位置window.Left = left;window.Top = top;//改成 w.Show(); 可測試非模態窗口window.Show();
}
<Grid><Button x:Name="buttonMT" Content="模態窗口" HorizontalAlignment="Left" Margin="111,107,0,0" VerticalAlignment="Top" Height="94" Width="189" Click="buttonMT_Click"/><Button x:Name="buttonFMT" Content="非模態窗口" HorizontalAlignment="Left" Margin="481,107,0,0" VerticalAlignment="Top" Height="94" Width="189" Click="buttonFMT_Click"/>
</Grid>
二、Page
1.概述
Page類 表示可導航的頁面,一般和 Frame 或 NavigationWindow 搭配使用。
官方文檔:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.page?view=netframework-4.8
2.Page類
Page類的部分屬性如下:
三、Frame
1.概述
Frame類 是一種支持導航頁面的內容控件,內部可以顯示Page。
官方文檔:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.frame?view=netframework-4.8
2.Frame類
Frame類的部分屬性如下:
3.示例
public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();}private void button1_Click(object sender, RoutedEventArgs e){frame.Content = new Page1();}//Navigate有四種重載方式,此處演示兩種private void button2_Click(object sender, RoutedEventArgs e){frame.Navigate(new Uri("Page2.xaml",UriKind.RelativeOrAbsolute));}private void button3_Click(object sender, RoutedEventArgs e){frame.Navigate(new Page3());}private void buttonForword_Click(object sender, RoutedEventArgs e){if (frame.CanGoForward) {frame.GoForward();}}private void buttonBack_Click(object sender, RoutedEventArgs e){if (frame.CanGoBack){ frame.GoBack(); }}
}
<Grid><Frame x:Name="frame" Background="LightGray" Width="600" Height="300" Content="" Margin="100,10,100,125"/><Button x:Name="buttonForword" Content="返回" HorizontalAlignment="Left" Margin="100,337,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.288,0.263" Height="26" Width="71" Click="buttonForword_Click"/><Button x:Name="buttonBack" Content="翻頁" HorizontalAlignment="Left" Margin="629,337,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.288,0.263" Height="26" Width="71" Click="buttonBack_Click"/><Button x:Name="button1" Content="1" HorizontalAlignment="Left" Margin="312,337,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.288,0.263" Height="26" Width="38" Click="button1_Click"/><Button x:Name="button2" Content="2" HorizontalAlignment="Center" Margin="0,337,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.288,0.263" Height="26" Width="38" Click="button2_Click"/><Button x:Name="button3" Content="3" HorizontalAlignment="Left" Margin="451,337,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.288,0.263" Height="26" Width="38" Click="button3_Click"/></Grid>
四、ViewBox
1. 概述
ViewBox類,可以拉伸或縮放控件,使用這個控件使界面隨屏幕尺寸變化而自動縮放。
官方文檔:https://learn.microsoft.com/zh-cn/dotnet/api/system.windows.controls.viewbox?view=netframework-4.8
2. 詳解
ViewBox類的部分屬性如下所示
3. 示例
總結
- ShowDialog() 顯示窗口并等待窗口關閉后才返回(模態),Show() 顯示窗口并返回(非模態)
- ViewBox可使控件內的子控件同步拉伸放大或縮小