From 6fc5ca253e4840e29e19a198d0a2c38b19fb679c Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Mon, 23 Jun 2025 23:05:34 +0800 Subject: [PATCH] =?UTF-8?q?:tada:=20=E6=95=B0=E6=8D=AE=E6=BA=90=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Base-2-Window/Base-2-Window.csproj | 10 +++ .../Base-2-Window/MVVM/ViewModelBase.cs | 14 ++++ .../SerialPort/Base-2-Window/MainWindow.xaml | 72 ++++++++++++++++--- .../Base-2-Window/MainWindow.xaml.cs | 23 +++--- .../Base-2-Window/Model/SerialPortModel.cs | 14 ++++ .../ViewModel/MainWindowViewModel.cs | 17 +++++ .../SerialPort.sln.DotSettings.user | 4 ++ 7 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 CSharp/SerialPort/Base-2-Window/MVVM/ViewModelBase.cs create mode 100644 CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs create mode 100644 CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs create mode 100644 CSharp/SerialPort/SerialPort.sln.DotSettings.user diff --git a/CSharp/SerialPort/Base-2-Window/Base-2-Window.csproj b/CSharp/SerialPort/Base-2-Window/Base-2-Window.csproj index 5cc54ea..c4fbee4 100644 --- a/CSharp/SerialPort/Base-2-Window/Base-2-Window.csproj +++ b/CSharp/SerialPort/Base-2-Window/Base-2-Window.csproj @@ -9,4 +9,14 @@ true + + + ..\..\..\..\..\..\..\software\Plugins\nuget\system.io.ports\5.0.1\ref\netstandard2.0\System.IO.Ports.dll + + + + + + + diff --git a/CSharp/SerialPort/Base-2-Window/MVVM/ViewModelBase.cs b/CSharp/SerialPort/Base-2-Window/MVVM/ViewModelBase.cs new file mode 100644 index 0000000..5fd8424 --- /dev/null +++ b/CSharp/SerialPort/Base-2-Window/MVVM/ViewModelBase.cs @@ -0,0 +1,14 @@ +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace Base_2_Window.MVVM; + +public class ViewModelBase : INotifyPropertyChanged +{ + public event PropertyChangedEventHandler? PropertyChanged; + + protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } +} \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml index 04855b3..c369293 100644 --- a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml +++ b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml @@ -3,17 +3,71 @@ 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:Base_2_Window" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> - - - - + + + + - - XXX - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs index c57fc25..7b7d245 100644 --- a/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs +++ b/CSharp/SerialPort/Base-2-Window/MainWindow.xaml.cs @@ -1,23 +1,26 @@ -using System.Text; +using System.IO.Ports; 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; +using Base_2_Window.ViewModel; namespace Base_2_Window; /// -/// Interaction logic for MainWindow.xaml +/// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); + + DataContext = new MainWindowViewModel + { + SerialPortNames = SerialPort.GetPortNames().Select(s => s).ToList(), + BaudRates = new List { 9600 }, + DataBits = new List { 6, 7, 8 }, + StopBitsList = new List { StopBits.None, StopBits.One, StopBits.Two, StopBits.OnePointFive }, + ParityList = new List + { Parity.None, Parity.Even, Parity.Mark, Parity.None, Parity.Odd, Parity.Space } + }; } } \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs b/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs new file mode 100644 index 0000000..57f79e2 --- /dev/null +++ b/CSharp/SerialPort/Base-2-Window/Model/SerialPortModel.cs @@ -0,0 +1,14 @@ +using System.IO.Ports; + +namespace Base_2_Window.Model; + +public class SerialPortModel +{ + public List? SerialPortNames { get; set; } + + public int? BaudRate { get; set; } + + public int? DataBits { get; set; } + + public StopBits? StopBits { get; set; } +} \ No newline at end of file diff --git a/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs b/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs new file mode 100644 index 0000000..38e11dd --- /dev/null +++ b/CSharp/SerialPort/Base-2-Window/ViewModel/MainWindowViewModel.cs @@ -0,0 +1,17 @@ +using System.IO.Ports; +using Base_2_Window.MVVM; + +namespace Base_2_Window.ViewModel; + +public class MainWindowViewModel : ViewModelBase +{ + public List? SerialPortNames { get; set; } + + public List? BaudRates { get; set; } + + public List? StopBitsList { get; set; } + + public List? ParityList { get; set; } + + public List? DataBits { get; set; } +} \ No newline at end of file diff --git a/CSharp/SerialPort/SerialPort.sln.DotSettings.user b/CSharp/SerialPort/SerialPort.sln.DotSettings.user new file mode 100644 index 0000000..bba4a65 --- /dev/null +++ b/CSharp/SerialPort/SerialPort.sln.DotSettings.user @@ -0,0 +1,4 @@ + + ForceIncluded + ForceIncluded + ForceIncluded \ No newline at end of file