官網
https://visualstudio.microsoft.com/zh-hans/vs/

文檔
https://docs.microsoft.com/zh-cn/visualstudio/designers/getting-started-with-wpf?view=vs-2019


第一個WPF桌面應用程序
安裝Visual Studio

創建應用程序項目


工作區大概長這樣子

寫代碼測試
-
編輯MainWindow.xaml(類似安卓開發的xml語言)

在Grid布局中添加
<TextBlock HorizontalAlignment="Left" Margin="252,47,0,0" TextWrapping="Wrap" Text="Select a message option and then choose the Display button." VerticalAlignment="Top"/>
<RadioButton x:Name="HelloButton" Content="Hello" IsChecked="True" HorizontalAlignment="Left" Margin="297,161,0,0" VerticalAlignment="Top" Height="30.24" Width="78.107"/>
<RadioButton x:Name="GoodbyeButton" Content="Goodbye" HorizontalAlignment="Left" Margin="488,161,0,0" VerticalAlignment="Top"/>
<Button Content="Display" Click="Button_Click_1" HorizontalAlignment="Left" Height="40.061" Margin="346.107,245.449,0,0" VerticalAlignment="Top" Width="201.689"/>

(可進行拖拽,放大縮小的圖形化操作)
-
在MainWindow.xmal.ca添加按鈕事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (HelloButton.IsChecked == true)
{
MessageBox.Show("Hello.","提示");
}
else if (GoodbyeButton.IsChecked == true)
{
MessageBox.Show("Goodbye.","提示");
}
}
}
}
其中Button_Click_1與之前xaml中Click對應

-
啟動



小結
C# 開發的WPF對於非專業的開發者可以作為編寫小工具的加持,畢竟現在PC桌面開發市場並不景氣。
但是拋開市場經濟的影響,C#加標記語言的開發模式和Android開發有着異曲同工之妙,殊途同歸的設計除了適應場景上的不同,不知道是否會帶來對於編程本身更加深刻的認知。
就像剛過中秋的月餅一樣,如果擺在面前的有10種月餅,每種月餅有10個,但是只能吃十個,假設從來沒有嘗過月餅的你是否會每種都去嘗試一下呢?
