Loading... ## 一、目前进展 目前已全部完成**快捷导航**所有功能,包括分类和子项的增、删。 由于界面实在太丑(自己都看不下去了),决心对UI进行重构,并采用模块化的方式进行各功能模块开发。 以下是目前项目情况介绍。 ## 二、项目结构 ### 1.文件结构  ### 2.类结构  总体上使用prism库完成MVVM和各模型间通信。 * 界面:包括MainView、AddBoxDialog、DelBoxDialog以及各类消息提示框。 * 模型:包括MainViewModel、AddBoxViewModel、DelBoxViewModel及各类消息提示框对应模型。 * 通信:prism库采用订阅及发布的方式进行各模块间消息传递。目前使用的消息包括AddBoxEvent和DelBoxEvent。 ## 三、增加和删除功能实现 ### 1.增加 UI层: ```xml <UserControl x:Class="SoftMa.View.Dialogs.AddBoxDialog" 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" xmlns:local="clr-namespace:SoftMa.View.Dialogs" mc:Ignorable="d" Height="150" Width="300"> <prism:Dialog.WindowStyle> <Style TargetType="Window"> <Setter Property="ShowInTaskbar" Value="False"/> <Setter Property="SizeToContent" Value="WidthAndHeight"/> <Setter Property="ResizeMode" Value="NoResize"/> <Setter Property="WindowStyle" Value="None"/> </Style> </prism:Dialog.WindowStyle> <Border Margin="2" BorderBrush="Red"> <Grid x:Name="LayoutRoot" Margin="5"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <mah:MetroHeader Margin="2,2,2,10" Header="配置"> <mah:MetroHeader.HeaderTemplate> <DataTemplate> <StackPanel VerticalAlignment="Center" Orientation="Horizontal"> <iconPacks:PackIconMaterial VerticalAlignment="Center" Kind="FaceProfile" /> <TextBlock Margin="2 0 0 0" VerticalAlignment="Center" Text="{Binding}" /> </StackPanel> </DataTemplate> </mah:MetroHeader.HeaderTemplate> <TextBox ContextMenu="{x:Null}" Grid.Row="1" mah:TextBoxHelper.ClearTextButton="True" mah:TextBoxHelper.Watermark="{Binding Message}" Text="{Binding Context}" mah:TextBoxHelper.WatermarkAlignment="Left" HorizontalAlignment="Center" mah:TextBoxHelper.UseFloatingWatermark="True" HorizontalContentAlignment="Stretch" Width="240" Margin="2 20 2 10"/> </mah:MetroHeader> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Margin="5" Foreground="White" FontSize="12" Background="#5cb85c" Command="{Binding CloseDialogCommand}" CommandParameter="true" Content="确定" Width="64" Height="28" HorizontalAlignment="Right" Grid.Row="1"/> <Button Grid.Column="1" Margin="5" Foreground="White" FontSize="12" Background="#d9534f" Command="{Binding CloseDialogCommand}" CommandParameter="false" Content="取消" Width="64" Height="28" HorizontalAlignment="Left" Grid.Row="1"/> </Grid> </Grid> </Border> </UserControl> ``` 模型层: ```c# using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Services.Dialogs; using SoftMa.Events; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoftMa.Model { public class AddBoxViewModel : BindableBase, IDialogAware { IEventAggregator _eventAggregator; private DelegateCommand<string> _closeDialogCommand; public DelegateCommand<string> CloseDialogCommand => _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog)); private string _message; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } private string _title = "添加"; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } private string _context; public string Context { get { return _context; } set { SetProperty(ref _context, value); } } public event Action<IDialogResult> RequestClose; protected virtual void CloseDialog(string parameter) { ButtonResult result = ButtonResult.None; if (parameter?.ToLower() == "true") { result = ButtonResult.OK; _eventAggregator.GetEvent<AddBoxEvent>().Publish(Context); } else if (parameter?.ToLower() == "false") result = ButtonResult.Cancel; RaiseRequestClose(new DialogResult(result)); } public virtual void RaiseRequestClose(IDialogResult dialogResult) { RequestClose?.Invoke(dialogResult); } public virtual bool CanCloseDialog() { return true; } public virtual void OnDialogClosed() { } public virtual void OnDialogOpened(IDialogParameters parameters) { Message = parameters.GetValue<string>("message"); } public AddBoxViewModel(IEventAggregator ea) { _eventAggregator = ea; } } } ``` 因为`IDialogAware`类只能返回`IDialogResult`类型结果,而无法返回自定义数据类型,导致额外加了`_eventAggregator.GetEvent<AddBoxEvent>().Publish(Context)`进行消息传递。 ### 2.删除 UI层: ```xml <UserControl x:Class="SoftMa.View.Dialogs.DelBoxDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" Width="300" Height="150"> <prism:Dialog.WindowStyle> <Style TargetType="Window"> <Setter Property="ShowInTaskbar" Value="False"/> <Setter Property="SizeToContent" Value="WidthAndHeight"/> <Setter Property="ResizeMode" Value="NoResize"/> <Setter Property="WindowStyle" Value="None"/> </Style> </prism:Dialog.WindowStyle> <Border Margin="2" BorderBrush="Red"> <Grid x:Name="LayoutRoot" Margin="5"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <mah:MetroHeader Margin="2,2,2,10" Header="配置:请选择需要删除的项"> <mah:MetroHeader.HeaderTemplate> <DataTemplate> <StackPanel VerticalAlignment="Center" Orientation="Horizontal"> <iconPacks:PackIconMaterial VerticalAlignment="Center" Kind="FaceProfile" /> <TextBlock Margin="2 0 0 0" VerticalAlignment="Center" Text="{Binding}" /> </StackPanel> </DataTemplate> </mah:MetroHeader.HeaderTemplate> <ComboBox ItemsSource="{Binding BoxItems}" IsEditable="false" SelectedIndex="{Binding SelIndex}" Width="240" Margin="2 20 2 10"/> </mah:MetroHeader> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Margin="5" Foreground="White" FontSize="12" Background="#5cb85c" Command="{Binding CloseDialogCommand}" CommandParameter="true" Content="确定" Width="64" Height="28" HorizontalAlignment="Right" Grid.Row="1"/> <Button Grid.Column="1" Margin="5" Foreground="White" FontSize="12" Background="#d9534f" Command="{Binding CloseDialogCommand}" CommandParameter="false" Content="取消" Width="64" Height="28" HorizontalAlignment="Left" Grid.Row="1"/> </Grid> </Grid> </Border> </UserControl> ``` 模型层: ```c# using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Services.Dialogs; using SoftMa.Events; namespace SoftMa.Model.Dialogs { class DelBoxViewModel : BindableBase, IDialogAware { private List<string> _boxItems; public List<string> BoxItems { get { return _boxItems; } set { SetProperty(ref _boxItems, value); } } private int _selIndex; public int SelIndex { get { return _selIndex; } set { SetProperty(ref _selIndex, value); } } IEventAggregator _eventAggregator; private DelegateCommand<string> _closeDialogCommand; public DelegateCommand<string> CloseDialogCommand => _closeDialogCommand ?? (_closeDialogCommand = new DelegateCommand<string>(CloseDialog)); private string _message; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } private string _title = "添加"; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } private string _context; public string Context { get { return _context; } set { SetProperty(ref _context, value); } } public event Action<IDialogResult> RequestClose; protected virtual void CloseDialog(string parameter) { ButtonResult result = ButtonResult.None; if (parameter?.ToLower() == "true") { result = ButtonResult.OK; if(SelIndex>=0) { _eventAggregator.GetEvent<DelBoxEvent>().Publish(BoxItems.ElementAt(SelIndex)); } else { _eventAggregator.GetEvent<DelBoxEvent>().Publish(""); } } else if (parameter?.ToLower() == "false") { result = ButtonResult.Cancel; } RaiseRequestClose(new DialogResult(result)); } public virtual void RaiseRequestClose(IDialogResult dialogResult) { RequestClose?.Invoke(dialogResult); } public virtual bool CanCloseDialog() { return true; } public virtual void OnDialogClosed() { } public virtual void OnDialogOpened(IDialogParameters parameters) { SelIndex = -1; //Message = parameters.GetValue<string>("message"); var tmplist = parameters.GetValue<string>("boxitems"); BoxItems = new List<string>(tmplist.Split(";")); } public DelBoxViewModel(IEventAggregator ea) { _eventAggregator = ea; } } } ``` ## 四、演示   ## 五、下一步计划 ### 1.整体设计 * 采用模块化设计,各模块自成单独的一个项目,互不干扰。 * 将已完成功能全部集成到单独的Navi(导航)模块。 * 开展基于ESP8266芯片的物联网接入。 ### 2.模块开发 * Navi(导航):即将开展重构; * IoT(物联网):即将展开; * 工具集(编码转换、随机数(字符生成)、json格式校验等):下下一步计划; * 加解密:下下一步计划; * js/c#/lua/python脚本支持:遥遥无期。 最后修改:2022 年 04 月 23 日 11 : 45 PM © 禁止转载