105040 Update 关闭窗口时增加提示,未保存电机数据是否丢弃

添加未保存警告窗口及其相关视图模型和消息类

增加了 `LectotypeMessage.cs` 文件:
- 定义了 `LectotypeMessage` 类,继承自 `ValueChangedMessage<Dictionary<string,string>>`。

增加了 `NotSavedWarningViewModel.cs` 文件:
- 定义了 `NotSavedWarningViewModel` 类,实现了 `INotifyPropertyChanged` 和 `IDialogResultable<bool>` 接口。
- 包含 `PropertyChanged` 事件、`OnChange` 方法、`NotSavedLectotypeList` 属性、`Result` 和 `CloseAction` 属性及 `Close` 方法。

增加了 `NotSavedWarningWindow.xaml` 文件:
- 定义了未保存警告窗口的 UI,包括 `Border`、`Grid`、`StackPanel`、`TextBlock`、`ListBox` 和两个按钮。

增加了 `NotSavedWarningWindow.xaml.cs` 文件:
- 定义了 `NotSavedWarningWindow` 类,包含 `ViewModel` 字段、构造函数及按钮点击事件处理方法。
This commit is contained in:
lihanbo 2025-01-23 17:06:34 +08:00
parent 707cf928ee
commit 860ce040ed
13 changed files with 295 additions and 32 deletions

View File

@ -0,0 +1,16 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sinvo.EplanHpD.Plugin.WPFUI.Common
{
internal class LectotypeMessage : ValueChangedMessage<Dictionary<string,string>>
{
public LectotypeMessage(Dictionary<string, string> value) : base(value)
{
}
}
}

View File

@ -152,6 +152,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\CommonMessage.cs" />
<Compile Include="Common\LectotypeMessage.cs" />
<Compile Include="Common\OpenDrawMessage.cs" />
<Compile Include="Converter\ConnectionTypeConverter.cs" />
<Compile Include="Converter\FlagEnumConverter.cs" />
@ -164,6 +165,7 @@
<Compile Include="Extension\ApplicationExt.cs" />
<Compile Include="Extension\BaseAppExt.cs" />
<Compile Include="Extension\CheckedModelExt.cs" />
<Compile Include="Extension\MotorModelUniqueFlagExt.cs" />
<Compile Include="Extension\PropertyListExt.cs" />
<Compile Include="Models\CheckedModel.cs" />
<Compile Include="Models\ConfigItemModel.cs" />
@ -198,6 +200,10 @@
<Compile Include="View\CableLectotypeUserControl.xaml.cs">
<DependentUpon>CableLectotypeUserControl.xaml</DependentUpon>
</Compile>
<Compile Include="View\Dialog\NotSavedWarningViewModel.cs" />
<Compile Include="View\Dialog\NotSavedWarningWindow.xaml.cs">
<DependentUpon>NotSavedWarningWindow.xaml</DependentUpon>
</Compile>
<Compile Include="View\LayoutHelperWindow.xaml.cs">
<DependentUpon>LayoutHelperWindow.xaml</DependentUpon>
</Compile>
@ -225,6 +231,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\Dialog\NotSavedWarningWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="View\LayoutHelperWindow.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>

View File

@ -321,17 +321,17 @@
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--<DataGridTemplateColumn Header="是否布线完成">
<DataGridTemplateColumn Header="是否布线完成">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding IsComplete, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="True" />
IsEnabled="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->
</DataGridTemplateColumn>
<DataGridTextColumn
Binding="{Binding CableModelNo}"
Header="型号"

View File

@ -20,6 +20,7 @@ using System.Windows.Input;
using static Sinvo.EplanHpD.Plugin.WPFUI.Utils.LectotypeManager;
using System.Linq;
using EPLAN.Harness.ProjectCore.Occurrences;
using Sinvo.EplanHpD.Plugin.WPFUI.Extension;
namespace Sinvo.EplanHpD.Plugin.WPFUI.View
{
@ -91,6 +92,15 @@ namespace Sinvo.EplanHpD.Plugin.WPFUI.View
private void CableratorBtn_Click(object sender, RoutedEventArgs e)
{
ViewModel.Cablerator();
//var motor = Motor as MotorModel;
if(Motor is CableLectotypeViewModel motor)
{
WeakReferenceMessenger.Default.Send<LectotypeMessage>(new LectotypeMessage
(new System.Collections.Generic.Dictionary<string, string>
{
{motor.Motor.MotorModelStr,motor.Motor.AxisNo}
}));
}
}
private void ListView_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)

View File

@ -0,0 +1,44 @@
using CommunityToolkit.Mvvm.Input;
using HandyControl.Tools.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog
{
public class NotSavedWarningViewModel : INotifyPropertyChanged, IDialogResultable<bool>
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnChange([CallerMemberName] string propName = null)
{
PropertyChanged?.Invoke(propName, new PropertyChangedEventArgs(propName));
}
private Dictionary<string, string> _notSavedLectotypeList = [];
public Dictionary<string, string> NotSavedLectotypeList
{
get => _notSavedLectotypeList;
set
{
_notSavedLectotypeList = value;
OnChange();
}
}
public bool Result { get ; set ; }
public Action CloseAction { get; set; }
internal void Close(bool result)
{
Result = result;
CloseAction?.Invoke();
}
}
}

View File

@ -0,0 +1,54 @@
<Border
x:Class="Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog.NotSavedWarningWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Width="800"
Height="450"
Background="#f5f5f5"
CornerRadius="5"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="60" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Vertical">
<TextBlock
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20"
Text="以下轴号的选型数据未保存,是否继续操作?继续操作将丢失未保存的数据!"
TextWrapping="Wrap" />
<ListBox
Height="300"
Margin="10"
ItemsSource="{Binding NotSavedLectotypeList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="5,0" Text="{Binding Key}" />
<TextBlock Margin="5,0" Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
<StackPanel Grid.Row="1">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button
Width="100"
Click="btnOK_Click"
Content="是" />
<Button
Width="100"
Click="btnCancel_Click"
Content="否" />
</StackPanel>
</StackPanel>
</Grid>
</Border>

View File

@ -0,0 +1,44 @@
using HandyControl.Tools.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
namespace Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog
{
/// <summary>
/// NotSavedWarningWindow.xaml 的交互逻辑
/// </summary>
public partial class NotSavedWarningWindow
{
public NotSavedWarningViewModel ViewModel;
public NotSavedWarningWindow(Dictionary<string, string> notSavedLectotypeList)
{
InitializeComponent();
this.DataContext = ViewModel = new NotSavedWarningViewModel();
ViewModel.NotSavedLectotypeList = notSavedLectotypeList;
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
ViewModel.Close(true);
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
ViewModel.Close(false);
}
}
}

View File

@ -16,6 +16,7 @@
Height="800"
d:DataContext="{d:DesignInstance Type=viewmodel:LectotypeViewModel}"
Closed="Window_Closed"
Closing="Window_Closing"
Loaded="Window_Loaded"
Topmost="False"
WindowStartupLocation="CenterScreen"
@ -99,10 +100,7 @@
HorizontalAlignment="Center"
FontSize="16"
Text="加载中..." />
<ProgressBar
Width="200"
Height="10"
IsIndeterminate="True" />
<hc:LoadingCircle />
</StackPanel>
</Grid>
</DataTemplate>

View File

@ -5,6 +5,7 @@ using EPLAN.Harness.Core.Extensions;
using EPLAN.Harness.Core.LibEntities;
using EPLAN.Harness.Core.Library;
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;
using Sinvo.EplanHpD.Plugin.WPFUI.Common;
@ -12,6 +13,7 @@ using Sinvo.EplanHpD.Plugin.WPFUI.Extension;
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
using Sinvo.EplanHpD.Plugin.WPFUI.Utils;
using Sinvo.EplanHpD.Plugin.WPFUI.View;
using Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog;
using Sinvo.EplanHpD.Plugin.WPFUI.ViewModel;
using System;
using System.Collections.Generic;
@ -58,6 +60,20 @@ namespace Sinvo.EplanHpD.Plugin.WPFUI
DrawPDFDrawer.IsOpen = true;
}
});
WeakReferenceMessenger.Default.Register<LectotypeMessage>("Lectotype", (sender, message) =>
{
if (message.Value != null)
{
ViewModel.AddNotSavedLectotype(message.Value);
}
});
WeakReferenceMessenger.Default.Register<LectotypeMessage>("LectotypeSaved", (sender, message) =>
{
if(message.Value != null)
{
ViewModel.RemoveNotSavedLectotype(message.Value);
}
});
Growl.Register("CableLectotypeMessage", GrowlParent);
}
@ -181,8 +197,12 @@ namespace Sinvo.EplanHpD.Plugin.WPFUI
private void Window_Closed(object sender, System.EventArgs e)
{
//DrawPDFHelper.ClearCache();
WeakReferenceMessenger.Default.Unregister<CommonMessage>("RowDetailsVisibility");
WeakReferenceMessenger.Default.Unregister<OpenDrawMessage>("OpenDraw");
WeakReferenceMessenger.Default.Unregister<LectotypeMessage>("Lectotype");
WeakReferenceMessenger.Default.Unregister<LectotypeMessage>("LectotypeSaved");
MotorExcelHelper.Instance.CloseStream();
}
@ -369,21 +389,56 @@ namespace Sinvo.EplanHpD.Plugin.WPFUI
ViewModel.ToMotorSource(motorModelPartId, "");
}
}
private void StartLayoutBtn_Click(object sender, RoutedEventArgs e)
{
var motorFlags = ViewModel.Motors.Where(motor => !motor.IsError).Select(motor => motor.GetUniqueFlag()).ToList();
var window = new LayoutHelperWindow(motorFlags);
//window.MotorIds = motorIds;
ElementHost.EnableModelessKeyboardInterop(window);
var mainApp = BaseApp.ActiveApplication;
var helper = new System.Windows.Interop.WindowInteropHelper(window);
helper.Owner = mainApp.Handle;
window.Show();
this.Close();
if (ViewModel.GetNotSavedLectotypeList() != null && ViewModel.GetNotSavedLectotypeList().Any())
{
CloseBefore(() =>
{
ViewModel.ClearNotSavedLectotypeList();
var motorFlags = ViewModel.Motors.Where(motor => !motor.IsError).Select(motor => motor.GetUniqueFlag()).ToList();
var window = new LayoutHelperWindow(motorFlags);
//window.MotorIds = motorIds;
ElementHost.EnableModelessKeyboardInterop(window);
var mainApp = BaseApp.ActiveApplication;
var helper = new System.Windows.Interop.WindowInteropHelper(window);
helper.Owner = mainApp.Handle;
window.Show();
this.Close();
});
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(ViewModel.GetNotSavedLectotypeList() != null && ViewModel.GetNotSavedLectotypeList().Any())
{
e.Cancel = true;
CloseBefore(() =>
{
ViewModel.ClearNotSavedLectotypeList();
this.Close();
});
}
}
/// <summary>
/// 关闭前事件
/// </summary>
/// <param name="closeAction">关闭时要执行的操作</param>
private void CloseBefore(Action closeAction)
{
HandyControl.Controls.Dialog.Show(new NotSavedWarningWindow(ViewModel.GetNotSavedLectotypeList()))
.Initialize<NotSavedWarningViewModel>(vm => vm.Result = false)
.GetResultAsync<bool>()
.ContinueWith(x => {
this.Dispatcher.BeginInvoke(() =>
{
if (x.Result)
{
closeAction();
}
});
});
}
}
}

View File

@ -27,7 +27,6 @@
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
<converter:FlagEnumConverter x:Key="FlagEnumConverter" />
<converter:NameTypeConverter x:Key="NameTypeConverter" />
<DataTemplate x:Key="LoadingMask">
@ -48,7 +47,6 @@
</Grid>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>

View File

@ -25,7 +25,7 @@
<Setter Property="FontWeight" Value="ExtraBold" />
</Style>
</Window.Resources>
<Grid x:Name="top">
<Grid x:Name="top" Margin="10">
<Grid.RowDefinitions>

View File

@ -57,12 +57,7 @@
Content="自动隐藏其他线"
FontSize="16"
IsChecked="{Binding OthersWireShow}" />
<CheckBox
x:Name="ContinueScan"
Margin="3"
Content="连续扫描"
FontSize="16"
IsChecked="{Binding ContinueScan}" />
<!--<CheckBox Content="窗口透明" FontSize="16" />-->
<CheckBox
x:Name="TopMostWindow"
@ -72,12 +67,17 @@
FontSize="16"
IsChecked="True"
Unchecked="TopMostWindow_Unchecked" />
<CheckBox
x:Name="ContinueScan"
Margin="3"
Content="连续扫描"
FontSize="16"
IsChecked="{Binding ContinueScan}" />
<!--<CheckBox
Margin="3"
Content="扫描即装配"
FontSize="16"
IsChecked="{Binding ScanAndAssembled}" />
IsChecked="{Binding ScanAndAssembled}" />-->
<CheckBox
Margin="3"
Content="隐藏或显示时同时包括注释"

View File

@ -5,10 +5,12 @@ using EPLAN.Harness.Core.Utils;
using EPLAN.Harness.ProjectCore;
using EPLAN.Harness.ProjectCore.Occurrences;
using EPLAN.Harness.ProjectCore.Occurrences.Designer;
using HandyControl.Tools.Extension;
using Sinvo.EplanHpD.Plugin.WPFUI.Datas;
using Sinvo.EplanHpD.Plugin.WPFUI.Extension;
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
using Sinvo.EplanHpD.Plugin.WPFUI.Utils;
using Sinvo.EplanHpD.Plugin.WPFUI.View.Dialog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@ -71,7 +73,7 @@ public class LectotypeViewModel(string docId) : INotifyPropertyChanged
Debug.Print($"OnSelectedItemChange :{stopwatch.ElapsedMilliseconds}ms");
}
}
private Dictionary<string, string> NotSavedLectotypeList = [];
private ObservableCollection<LectotypeLineModel> _selectedSubItem = [];
@ -723,4 +725,36 @@ public class LectotypeViewModel(string docId) : INotifyPropertyChanged
});
MotorExcelHelper.Instance.SaveLinesToExcel(targetPath, Wires);
}
public void AddNotSavedLectotype(Dictionary<string,string> value)
{
value.ForEach(it => NotSavedLectotypeList.Add(it.Key, it.Value));
}
public void RemoveNotSavedLectotype(Dictionary<string, string> value)
{
value.ForEach(it => NotSavedLectotypeList.Remove(it.Key));
}
internal async Task<bool> CheckSaved()
{
if(NotSavedLectotypeList != null && NotSavedLectotypeList.Any())
{
var isContinue = false;
return isContinue;
}
else
{
return false;
}
}
internal Dictionary<string, string> GetNotSavedLectotypeList()
{
return NotSavedLectotypeList;
}
internal void ClearNotSavedLectotypeList()
{
NotSavedLectotypeList?.Clear();
}
}