BOB/BOB/ViewModels/MainViewModel.cs
2025-12-04 17:11:48 +08:00

87 lines
2.2 KiB
C#

using BOB.Singleton;
using Common.PubEvent;
using Common.PubEvents;
using Logger;
using NLog;
using Prism.Dialogs;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Input;
namespace BOB.ViewModels
{
public class MainViewModel : BindableBase,INavigationAware
{
#region
#endregion
#region
#endregion
public DialogCloseListener RequestClose { get; set; }
private IEventAggregator _eventAggregator;
private IDialogService _dialogService;
private Devices _devices{ get; set; }
public MainViewModel(IEventAggregator eventAggregator, IDialogService dialogService,IContainerProvider containerProvider)
{
_dialogService = dialogService;
_eventAggregator = eventAggregator;
_devices=containerProvider.Resolve<Devices>();
}
private async Task InitializeDevicesAsync()
{
try
{
_devices.Init(SystemConfig.Instance.DeviceList);
await _devices.ConnectAllDevicesAsync();
_devices.InitCan();
_devices.StartPollingCollectionAsync();
}
catch (Exception ex)
{
LoggerHelper.ErrorWithNotify($"设备连接异常: {ex.Message}", ex.StackTrace);
}
finally
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(false);
}
}
#region
private bool _isInitialized = false;
public void OnNavigatedTo(NavigationContext navigationContext)
{
if (!_isInitialized)
{
_eventAggregator.GetEvent<OverlayEvent>().Publish(true);
_ = InitializeDevicesAsync();
_isInitialized = true;
}
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
#endregion
}
}