使用新框架与技术代替旧框架与技术,实现涂装车间后道标签扫码程序
This commit is contained in:
32
RIZO_Application.Modules.LogModule/LogModule.cs
Normal file
32
RIZO_Application.Modules.LogModule/LogModule.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Prism.Ioc;
|
||||
using Prism.Modularity;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using System.Reflection;
|
||||
using RIZO_Application.Core.Infrastructure.CustomExtensions;
|
||||
|
||||
namespace RIZO_Application.Modules.LogModule
|
||||
{
|
||||
public class LogModule : IModule
|
||||
{
|
||||
private readonly IRegionManager _regionManager;
|
||||
|
||||
public LogModule(IRegionManager regionManager)
|
||||
{
|
||||
_regionManager = regionManager;
|
||||
}
|
||||
|
||||
public void OnInitialized(IContainerProvider containerProvider)
|
||||
{
|
||||
_regionManager.RequestNavigate(RegionNames.LogRegion, "SystemLog");
|
||||
}
|
||||
|
||||
public void RegisterTypes(IContainerRegistry containerRegistry)
|
||||
{
|
||||
// 自动注册当前程序集中所有带 [AppService] 的类
|
||||
containerRegistry.AutoRegisterServices(Assembly.GetExecutingAssembly());
|
||||
// 自动注册当前程序集的所有带 [AutoRegisterView] 的视图
|
||||
containerRegistry.AutoRegisterViews(Assembly.GetExecutingAssembly());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Prism.Wpf" Version="8.1.97" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RIZO_Application.Models\RIZO_Application.Models.csproj" />
|
||||
<ProjectReference Include="..\RIZO_Application.Repository\RIZO_Application.Repository.csproj" />
|
||||
<ProjectReference Include="..\RIZO_Application\RIZO_Application.Core\RIZO_Application.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Views\SystemLog.xaml.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace RIZO_Application.Modules.LogModule.Services.Interfaces
|
||||
{
|
||||
public interface ILogService
|
||||
{
|
||||
string GetMessage();
|
||||
}
|
||||
}
|
||||
18
RIZO_Application.Modules.LogModule/Services/LogService.cs
Normal file
18
RIZO_Application.Modules.LogModule/Services/LogService.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using RIZO_Application.Infrastructure.CustomAttribute;
|
||||
using RIZO_Application.Infrastructure.Model;
|
||||
using RIZO_Application.Models;
|
||||
using RIZO_Application.Modules.LogModule.Services.Interfaces;
|
||||
using RIZO_Application.Repository;
|
||||
|
||||
namespace RIZO_Application.Modules.LogModule.Services
|
||||
{
|
||||
[AppService(ServiceType = typeof(ILogService), Lifetime = ServiceLifetime.Transient)]
|
||||
public class LogService : BaseRepository<User> ,ILogService
|
||||
{
|
||||
public string GetMessage()
|
||||
{
|
||||
|
||||
return AppSettings.Current.AppName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using NLog;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using RIZO_Application.Core.Mvvm;
|
||||
|
||||
namespace RIZO_Application.Modules.LogModule.ViewModels
|
||||
{
|
||||
public class SystemLogViewModel : RegionViewModelBase
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private SubscriptionToken _token;
|
||||
private readonly StringBuilder _logMessageBuilder = new StringBuilder();
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
private const int MaxLogLength = 1000; // 假设最大长度为1000,可以根据实际情况调整
|
||||
private const string LogFormat = "\n{0}: {1}";
|
||||
|
||||
// 定义一个日志更新事件
|
||||
public event EventHandler LogUpdated;
|
||||
|
||||
public string LogMessage
|
||||
{
|
||||
get { return _logMessageBuilder.ToString(); }
|
||||
set
|
||||
{
|
||||
_logMessageBuilder.Clear();
|
||||
_logMessageBuilder.Append(value);
|
||||
RaisePropertyChanged(nameof(LogMessage));
|
||||
}
|
||||
}
|
||||
|
||||
public SystemLogViewModel(
|
||||
IRegionManager regionManager,
|
||||
IEventAggregator eventAggregator)
|
||||
: base(regionManager)
|
||||
{
|
||||
OnLogReceived("初始化日志系统");
|
||||
_eventAggregator = eventAggregator;
|
||||
// 订阅事件,使用UI线程处理,保持强引用
|
||||
_token = _eventAggregator.GetEvent<SystemLogEvent>().Subscribe(OnLogReceived, ThreadOption.UIThread, true);
|
||||
}
|
||||
|
||||
private void OnLogReceived(string message)
|
||||
{
|
||||
string newLog = string.Format(LogFormat, DateTime.Now, message);
|
||||
_logMessageBuilder.Append(newLog);
|
||||
|
||||
if (_logMessageBuilder.Length > MaxLogLength)
|
||||
{
|
||||
// 计算需要截取的位置,保留最新的日志
|
||||
int startIndex = Math.Max(0, _logMessageBuilder.Length - MaxLogLength);
|
||||
_logMessageBuilder.Remove(0, startIndex);
|
||||
}
|
||||
|
||||
RaisePropertyChanged(nameof(LogMessage));
|
||||
_logger.Info(newLog);
|
||||
|
||||
// 触发日志更新事件
|
||||
OnLogUpdated();
|
||||
}
|
||||
|
||||
protected virtual void OnLogUpdated()
|
||||
{
|
||||
LogUpdated?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_token?.Dispose();
|
||||
}
|
||||
|
||||
public void AddTestLogMessage()
|
||||
{
|
||||
// 示例实现,可根据需求修改
|
||||
OnLogReceived("测试日志消息");
|
||||
}
|
||||
|
||||
public override void OnNavigatedTo(NavigationContext navigationContext)
|
||||
{
|
||||
//do something
|
||||
}
|
||||
}
|
||||
}
|
||||
24
RIZO_Application.Modules.LogModule/Views/SystemLog.xaml
Normal file
24
RIZO_Application.Modules.LogModule/Views/SystemLog.xaml
Normal file
@@ -0,0 +1,24 @@
|
||||
<UserControl x:Class="RIZO_Application.Modules.LogModule.Views.SystemLog"
|
||||
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:RIZO_Application.Modules.LogModule"
|
||||
xmlns:prism="http://prismlibrary.com/"
|
||||
prism:ViewModelLocator.AutoWireViewModel="True">
|
||||
<Grid>
|
||||
<GroupBox Header="系统日志" HorizontalAlignment="Stretch">
|
||||
<Grid>
|
||||
<TextBox
|
||||
x:Name="LogTextBox"
|
||||
Text="{Binding LogMessage}"
|
||||
TextWrapping="Wrap"
|
||||
AcceptsReturn="True"
|
||||
HorizontalAlignment="Stretch"
|
||||
VerticalScrollBarVisibility="Auto"
|
||||
IsReadOnly="True"
|
||||
/>
|
||||
</Grid>
|
||||
</GroupBox>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
46
RIZO_Application.Modules.LogModule/Views/SystemLog.xaml.cs
Normal file
46
RIZO_Application.Modules.LogModule/Views/SystemLog.xaml.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using RIZO_Application.Infrastructure.CustomAttribute;
|
||||
using RIZO_Application.Modules.LogModule.ViewModels;
|
||||
|
||||
namespace RIZO_Application.Modules.LogModule.Views
|
||||
{
|
||||
[AutoRegisterView(ViewName = "SystemLog")]
|
||||
/// <summary>
|
||||
/// SystemLog.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public partial class SystemLog : UserControl
|
||||
{
|
||||
public SystemLog()
|
||||
{
|
||||
|
||||
InitializeComponent();
|
||||
Loaded += SystemLogView_Loaded;
|
||||
}
|
||||
private void SystemLogView_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var viewModel = (SystemLogViewModel)DataContext;
|
||||
if (viewModel != null)
|
||||
{
|
||||
viewModel.LogUpdated += ViewModel_LogUpdated;
|
||||
// 初始加载时滚动到底部
|
||||
ScrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
private void ViewModel_LogUpdated(object sender, EventArgs e)
|
||||
{
|
||||
// 日志更新时滚动到底部(使用 Dispatcher 确保 UI 已更新)
|
||||
Dispatcher.Invoke(ScrollToBottom);
|
||||
}
|
||||
|
||||
private void ScrollToBottom()
|
||||
{
|
||||
if (LogTextBox != null && LogTextBox.Text != null)
|
||||
{
|
||||
LogTextBox.CaretIndex = LogTextBox.Text.Length; // 将光标移到末尾
|
||||
LogTextBox.ScrollToEnd(); // 滚动条滚动到最底部
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user