C# WPF設備監控軟件(經典)-升級篇

2024年2月6日 17点热度 0人点赞

這款設備監控軟件之前已經寫過兩篇文章講解過:

C# WPF設備監控軟件(經典)-上篇

C# WPF設備監控軟件(經典)-下篇

但是感覺還是有些不人性化的地方,今天在原有功能上再次進行優化升級了一下.

01

新增功能

① 設備報警消除時彈窗輸入用戶信息;

② 設備報警未清除的話,如果被縮小隱藏到托盤,十秒後又會自動彈出;

③ 單臺設備大小調整;

④ 界面底端增加報警信息和消除報警的日志記錄;

02

功能演示

03

代碼講解

信息確認窗口:

前臺xaml:

<Window x:Class="EquipmentMonitor.ConfirmView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  xmlns:local="clr-namespace:EquipmentMonitor" xmlns:cal="http://www.caliburnproject.org"  mc:Ignorable="d"  d:DesignHeight="450" d:DesignWidth="800" Height="150" Width="250" WindowStyle="None" WindowStartupLocation="CenterScreen"> <Window.Resources> <Style TargetType="Label"> <Setter Property="HorizontalAlignment" Value ="Center"/> <Setter Property="VerticalAlignment" Value ="Center"/> <Setter Property="FontSize" Value ="14"/> <Setter Property="FontWeight" Value ="Black"/></Style> <Style TargetType="Button"> <Setter Property="HorizontalAlignment" Value ="Center"/> <Setter Property="VerticalAlignment" Value ="Center"/> <Setter Property="FontSize" Value ="14"/> <Setter Property="FontWeight" Value ="Black"/></Style> </Window.Resources>  <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <Label Content="用戶:" Grid.Row="0" Grid.Column="0"/> <TextBox Text="{Binding UserInformation.UserName}" Grid.Row="0" Grid.Column="1" Height="25" Margin="5"/> <Label Content="信息:" Grid.Row="1" Grid.Column="0"/> <TextBox Text="{Binding UserInformation.Information}" Grid.Row="1" Grid.Column="1" Height="25" Margin="2"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Center"> <Button Height="25" MinWidth="100" Margin="5" Content="取消" cal:Message.Attach="[Event Click]=[CancleClick($source,$eventArgs)]"/> <Button Height="25" MinWidth="100" Margin="5" Content="確認" cal:Message.Attach="[Event Click]=[ConfirmClick($source,$eventArgs)]"/> </StackPanel> </Grid></Window>

後臺代碼:

using Caliburn.Micro;using EquipmentMonitor.Model;using PropertyChanged;using System;using System.Text;using System.Windows;
namespace EquipmentMonitor{ [AddINotifyPropertyChangedInterface] public class ConfirmViewModel:Screen,IViewModel { public UserInformation UserInformation { get; set; } = new UserInformation(); public void ConfirmClick(object sender, EventArgs e) { var str = ValidateLoginData(); if (!string.IsOrEmpty(str)) { MessageBox.Show(str); } else { this.TryCloseAsync(true); } } public void CancleClick(object sender, EventArgs e) { this.TryCloseAsync(false); }
public string ValidateLoginData() { StringBuilder sb = new StringBuilder(); if ( string.IsOrEmpty(UserInformation.Information)) { sb.AppendLine("確認信息不能為空,請檢查!"); } if (string.IsOrEmpty(UserInformation.UserName)) { sb.AppendLine("用戶名不能為空,請檢查!"); } return sb.ToString(); } }}

後臺主要是提供了模型數據和信息驗證的功能.如果用戶名稱和備註信息沒填寫,則提示用戶重新填寫,點擊了Cancle的話,則什麼也不做,報警未消除,信息正確填寫後,報警被消除。

然後在EquipmentViewModel.cs中調用這個窗口:這裡通過IWindowManager 獲取這個子窗口,如果信息正確,則記錄日志信息

ConfirmViewModel confirmViewModel = new ConfirmViewModel(); IWindowManager windowManager = IoC.Get<IWindowManager>(); var dialogResult = windowManager.ShowDialogAsync(confirmViewModel); if(dialogResult.Result != ) { bool result = Convert.ToBoolean(dialogResult.Result); if(result) { LogHelper.logWrite($"The {fileDTO.Title} Equipment Alarm is clear by {confirmViewModel.UserInformation.UserName},"   $"Remarks:{confirmViewModel.UserInformation.Information}", LogLevel.Warning); AlarmInfo  = $"{ DateTime.Now.ToLocalTime()}: The {fileDTO.Title} Equipment Alarm is clear by {confirmViewModel.UserInformation.UserName},"   $"Remarks:{confirmViewModel.UserInformation.Information} \n"; } else { return; } } }

底端日志信息較多,需要增加滾動條:TextBlockScrollViewer 包起來,並設置
VerticalScrollBarVisibility
="Auto"
HorizontalScrollBarVisibility
="Auto"就可以。

 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <TextBlock Text="{Binding AlarmInfo}" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Visible" /> </ScrollViewer>

此外:窗體啟動後讓顯示在屏幕中央:

WindowStartupLocation="CenterScreen"

窗體無邊框:

WindowStyle="None"

其它部分代碼在上節已經有展示講解,本節不再詳說。

04

源碼下載


百度網盤鏈接:
https://pan.baidu.com/s/1JTHtx3bfg8mMBVCKHRnSiw

提取碼:6666

小編微信:mm1552923

進群學習交流加 : mm1552923

如果喜歡我的文章,那麼

在看”和轉發是對我最大的支持!