在Windows平臺(tái)上開發(fā)桌面應(yīng)用程序時(shí),WinUI、WPF和WinForms是三種主要的用戶界面框架。每種框架都有其獨(dú)特的特點(diǎn)和適用場景。本文將通過示例代碼,詳細(xì)介紹這些框架的優(yōu)缺點(diǎn)及其適用場景,幫助dotnet桌面開發(fā)者更好地選擇適合自己項(xiàng)目的UI框架。
下表列出了 Windows 上最常用的應(yīng)用開發(fā)框架以及每個(gè)框架支持的功能:
WinUI(Windows UI Library)是Microsoft最新推出的UI框架,致力于為Windows 10和Windows 11應(yīng)用程序提供現(xiàn)代化的用戶界面。它使用Fluent Design System,支持最新的Windows功能。
示例代碼:選擇框架 WinUI 3
// MainWindow.xaml
<?xml version="1.0" encoding="utf-8"?>
<Window
x:Class="WinUISample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUISample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Hello, WinUI!" FontSize="32" Margin="0,0,20,0" />
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Window>
// MainWindow.xaml.cs
using Microsoft.UI.Xaml;
namespace WinUISample
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content="Clicked";
}
}
}
啟動(dòng)開發(fā)人員模式
執(zhí)行結(jié)果
優(yōu)點(diǎn):
缺點(diǎn):
WPF(Windows Presentation Foundation)是.NET Framework中的一部分,提供強(qiáng)大的圖形處理能力和數(shù)據(jù)綁定功能,適合構(gòu)建復(fù)雜的桌面應(yīng)用程序。
示例代碼:
<!-- MainWindow.xaml -->
<Window x:Class="WPFSample.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:WPFSample"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Hello, WPF!" FontSize="32" Margin="0,0,20,0" />
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
</StackPanel>
</Grid>
</Window>
// MainWindow.xaml.cs
using System.Windows;
namespace WPFSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
(sender as System.Windows.Controls.Button).Content="Clicked!";
}
}
}
執(zhí)行結(jié)果
優(yōu)點(diǎn):
缺點(diǎn):
WinForms(Windows Forms)是.NET Framework中較早引入的用戶界面框架,易于學(xué)習(xí)和使用,適合快速開發(fā)傳統(tǒng)的Windows桌面應(yīng)用程序。
示例代碼:
Program.cs
namespace WinFormsSample
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
Form1.cs
namespace WinFormsSample
{
public partial class Form1 : Form
{
private Button button;
public Form1()
{
InitializeComponent();
this.Text="WinForms Sample";
this.Width=800;
this.Height=600;
button=new Button();
button.Text="Click Me";
button.Width=200;
button.Height=60;
button.Location=new System.Drawing.Point(300, 300);
button.Click +=OnButtonClick;
this.Controls.Add(button);
}
private void OnButtonClick(object sender, EventArgs e)
{
button.Text="Clicked!";
}
}
}
執(zhí)行結(jié)果
優(yōu)點(diǎn):
缺點(diǎn):
選擇合適的UI框架取決于項(xiàng)目需求、開發(fā)團(tuán)隊(duì)的技術(shù)棧和預(yù)期的用戶體驗(yàn)。如果需要現(xiàn)代化的用戶界面和高性能,WinUI是不錯(cuò)的選擇;如果需要構(gòu)建復(fù)雜的數(shù)據(jù)驅(qū)動(dòng)應(yīng)用程序,WPF提供了強(qiáng)大的功能;如果需要快速開發(fā)傳統(tǒng)的桌面應(yīng)用程序,WinForms依然是一個(gè)可靠的選擇。
通過對(duì)WinUI、WPF和WinForms的對(duì)比,希望開發(fā)者能夠根據(jù)自己的需求做出最佳的選擇,構(gòu)建出優(yōu)質(zhì)的Windows桌面應(yīng)用程序。
如果本文對(duì)你有幫助,我將非常榮幸。
如果你本文有其他的看法,歡迎留言交流。
如果你喜歡我的文章,謝謝三連,點(diǎn)贊,關(guān)注,轉(zhuǎn)發(fā)吧!!!
#頭條創(chuàng)作挑戰(zhàn)賽# #記錄我的2024# #分享今日的感悟# #妙筆生花創(chuàng)作挑戰(zhàn)#
Windows 10 中,您可以使用以下批處理腳本來強(qiáng)化遠(yuǎn)程桌面mstsc連接的安全性,包括啟用強(qiáng)密碼、多因素身份驗(yàn)證、網(wǎng)絡(luò)級(jí)別身份驗(yàn)證、數(shù)據(jù)加密以及訪問控制和權(quán)限管理:
@echo off
rem 啟用強(qiáng)密碼和多因素身份驗(yàn)證
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v "LimitBlankPasswordUse" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v "MSV1_0\allownullpasswords" /t REG_DWORD /d 0 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v "RunAsPPL" /t REG_DWORD /d 1 /f
rem 啟用網(wǎng)絡(luò)級(jí)別身份驗(yàn)證
reg add "HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows NT\Terminal Services" /v "fEncryptRPCTraffic" /t REG_DWORD /d 1 /f
rem 啟用數(shù)據(jù)加密
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v "SecurityLayer" /t REG_DWORD /d 1 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v "UserAuthentication" /t REG_DWORD /d 1 /f
rem 訪問控制和權(quán)限管理(示例:限制只允許組中的用戶連接)
net localgroup "Remote Desktop Users" /add <用戶名>
echo 遠(yuǎn)程桌面連接安全性設(shè)置完成。
pause
請(qǐng)注意,這些設(shè)置需要管理員權(quán)限才能執(zhí)行。此批處理腳本將啟用強(qiáng)密碼和多因素身份驗(yàn)證、網(wǎng)絡(luò)級(jí)別身份驗(yàn)證、數(shù)據(jù)加密,并演示了如何通過命令行將用戶添加到“遠(yuǎn)程桌面用戶”組以實(shí)現(xiàn)訪問控制和權(quán)限管理。