C# WPF设备监控软件(经典)-升级篇

这款设备监控软件之前已经写过两篇文章讲解过:

C# WPF设备监控软件(经典)-上篇

C# WPF设备监控软件(经典)-下篇

但是感觉还是有些不人性化的地方,今天在原有功能上再次进行优化升级了一下。.

01—新增功能

① 设备报警消除时弹窗输入用户信息;

② 设备报警未清除的话,如果被缩小隐藏到托盘,十秒后又会自动弹出;

③ 单台设备大小调整;

④  界面底端增加报警信息和消除报警的日志记录;

02—功能演示

C# WPF设备监控软件(经典)-升级篇

03—代码讲解

信息确认窗口:

C# WPF设备监控软件(经典)-升级篇

前台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.IsNullOrEmpty(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.IsNullOrEmpty(UserInformation.Information))            {                sb.AppendLine("确认信息不能为空,请检查!");            }            if (string.IsNullOrEmpty(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 != null)                            {                                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;                                }                            }                        }

底端日志信息较多,需要增加滚动条:TextBlock 用ScrollViewer 包起来,并设置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