一個基於Net Core3.0的WPF框架Hello World實例


摘自:https://www.cnblogs.com/JerryMouseLi/p/11812972.html

一個基於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 框架依賴庫

依賴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:Class="IBMSManager.App" 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>

2.2.2 App.xaml.cs

App.xaml的后台文件,集成自System.Windows.Application,用於處理整個WPF應用程序相關的設置。

2.2.3 MainWindow.xaml

WPF應用程序界面與XAML設計文件

<Window x:Class="IBMSManager.MainWindow" 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>

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 效果如下

The Sky is the limit.
 
標簽:  WPF

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM