一個基于Net Core3.0的WPF框架Hello World實體
目錄- 一個基于Net Core3.0的WPF框架Hello World實體
- 1.創建WPF解決方案
- 1.1 創建Net Core版本的WPF工程
- 1.2 指定專案名稱,路徑,解決方案名稱
- 2. 依賴庫和4個程式檔案介紹
- 2.1 框架依賴庫
- 2.2 生成檔案說明
- 2.2.1 App.xaml
- 2.2.2 App.xaml.cs
- 2.2.3 MainWindow.xaml
- 2.2.4 MainWindow.xaml.cs
- 3. Hello World實體
- 3.1 拖動按鈕控制元件到WPF表單中
- 3.2 設計時中雙擊按鈕添加按鈕事件
- 3.3 事件處理函式中添加訊息提示框
- 3.4 效果如下
1.創建WPF解決方案
1.1 創建Net Core版本的WPF工程


1.2 指定專案名稱,路徑,解決方案名稱

2. 依賴庫和4個程式檔案介紹

2.1 框架依賴庫
依賴Microsoft.NETCore.App跟Microsoft.WindowsDesktop.App.WPF
2.2 生成檔案說明
生成4個檔案App.xaml,App.xaml.cs,MainWindow.xaml,MainWindow.xaml.cs
2.2.1 App.xaml
App.xaml設定應用程式的起始檔案與資源,這里的資源一般指:
- 其他xaml樣式檔案的路徑;
- 設定主題色,背景色,表單樣式;
- 按鈕樣式,選單樣式;
- 自定義彈出樣式,自定義滾動條寬度;
......等等
App.xaml檔案內容如下:
<Application x:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:IBMSManager"
StartupUri="MainWindow.xaml">
<Application.Resources>
系統資源定義區
</Application.Resources>
</Application>
- Application x: 表示Application后臺類
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 表示WPF應用程式的默認命名空間映射
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 映射可擴展應用程式標記語言(XAML)的擴展命名空間,通常將其映射為X前綴
- xmlns:local="clr-namespace:IBMSManager" 專案的名稱就是IBMSManager
- StartupUri="MainWindow.xaml" 表示要啟動的應用表單
2.2.2 App.xaml.cs
App.xaml的后臺檔案,集成自System.Windows.Application,用于處理整個WPF應用程式相關的設定,
2.2.3 MainWindow.xaml
WPF應用程式界面與XAML設計檔案
<Window x:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:IBMSManager"
mc:Ignorable="d"
Title="IBMSManager" Height="450" Width="800">
<Grid>
</Grid>
</Window>
- Windows物件實體基類
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"設計時的狀態的命名空間
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"標記兼容性相關的命名空間
- mc:Ignorable="d",d:DesignWidth是設計時的,所以,Ignorable="d"就是告訴編譯器在實際運行時,忽略設計時設定的值,
- Title="MainWindow" Height="450" Width="800" 屬性設定,這里講Title改為IBMSManager
- 用來劃分頁面的分割與區域,填放按鈕等頁面元素
2.2.4 MainWindow.xaml.cs
MainWindow.xaml的后臺檔案,集成自System.Windows.Window,用于撰寫MainWindow.xaml 的互動邏輯代碼
3. Hello World實體
3.1 拖動按鈕控制元件到WPF表單中


MainWindow.xaml檔案中會自動添加如下代碼
<Grid>
<Button Content="Button" HorizontalAlignment="Right" Margin="0,0,554,254" VerticalAlignment="Bottom"/>
</Grid>
代碼主要在Grid標簽中描述了按鈕的屬性
3.2 設計時中雙擊按鈕添加按鈕事件

MainWindow.xaml檔案中會自動添加Click="Button_Click
<Grid>
<Button Content="Button" HorizontalAlignment="Right" Margin="0,0,554,254" VerticalAlignment="Bottom" Click="Button_Click"/>
</Grid>
后臺MainWindow.xaml.cs檔案中自動添加了事件處理函式
private void Button_Click(object sender, RoutedEventArgs e)
{
}
3.3 事件處理函式中添加訊息提示框
點擊按鈕后,出現訊息提示框Hello World,
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Hello World!");
}
3.4 效果如下

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/22562.html
標籤:WPF
上一篇:《Head First C#》外星人入侵WPF撰寫原始碼
下一篇:wpf 兩個自定義控制元件
