PDMS & AM 侧边栏菜单 C# WPF技术

项目的完整下载地址

https://files.cnblogs.com/files/NanShengBlogs/AMCSTest.zip?t=1698491030&download=true

先看效果

 

下面先看实现的几个函数

1# 创建wpf的用户控件,无选项的参考此链接修改csproject文件

类库项目添加wpf方法

写入下列代码

<UserControl x:Class="AMCSTest.UserControl1"
             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:AMCSTest"
             mc:Ignorable="d" 
                     MinWidth="85"  MinHeight="250" Background="#BFDBFF" >
    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="Expander.StackPanel.Style" TargetType="FrameworkElement">
                <Setter Property="HorizontalAlignment" Value="Center"></Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid >
        <ItemsControl ItemsSource="{Binding Dict}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Expander   IsExpanded="False" Background="Transparent" FontWeight="Bold" IsEnabled="True" Foreground="Black" SnapsToDevicePixels="True" BorderBrush="Black" BorderThickness="0.8" Margin=" 0,2,0,2">
                        <Expander.Header>
                            <TextBlock Text="{Binding Path=Key}" TextWrapping="Wrap" FontWeight="ExtraBold" FontSize="12"/>
                        </Expander.Header>
                        <ScrollViewer VerticalScrollBarVisibility="Hidden" MaxHeight="300"  >
                            <StackPanel  HorizontalAlignment="Stretch" Margin=" 0,2,0,2" Orientation="Vertical">
                                <ItemsControl ItemsSource="{Binding Path=Value }">
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Button Style="{StaticResource Expander.StackPanel.Style}"  Tag="{Binding Path=Value}" Click="Button_Click" HorizontalAlignment="Stretch" Margin="0,1,0,1" Background="Transparent" Foreground="Black" FontStyle="Normal" FontWeight="Regular">
                                                <TextBlock Text="{Binding Path=Key}" TextWrapping="Wrap" FontSize="12">
                                                </TextBlock>
                                            </Button>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </StackPanel>
                        </ScrollViewer>
                    </Expander>
                    <!--<Button Content="{Binding Path=Key}" Tag="{Binding Path=Value}"
                 Click="Button_Click"  HorizontalAlignment="Stretch" Margin="0,1,0,1" Background="#FF1EB856" BorderBrush="#FF64D126" Grid.IsSharedSizeScope="False"/>-->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</UserControl>

usercontrol的交互代码

/// <summary>
/// UserControl1.xaml 的交互逻辑
/// </summary>
public partial class UserControl1 : UserControl
{
    public Command Cmd { get; set; }
    public WindowManager WinManager { get; set; }
    public UserControl1(WindowManager wm, Command _cmd)
    {
        InitializeComponent();
        this.WinManager = wm;
        this.Cmd = _cmd;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var btn = sender as Button;
        var mi = btn.Tag as MethodInfo;
        if (mi.DeclaringType == null) return;
        try
        {
            mi.Invoke(
           mi.IsStatic ? null : Activator.CreateInstance(mi.DeclaringType),
           new object[] { WinManager});
        }
        catch (Exception ex)
        {
            Interaction.MsgBox(ex.StackTrace);
        }   
    }
}

框架代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

//引用AM的dll文件,在AM的安装目录下面

using Aveva.ApplicationFramework;
using Aveva.ApplicationFramework.Presentation;

using Microsoft.VisualBasic;

namespace AMCSTest
{
    public class MianClass : IAddin
    {
        public string Description => base.GetType().FullName;
        public string Name => base.GetType().FullName;
        public void Start(ServiceManager serviceManager)
        {
            var winManager = serviceManager.GetService(typeof(WindowManager)) as WindowManager;
            结构工具 dwcmd = new 结构工具(winManager);
           // var cmdmanager = serviceManager.GetService(typeof(CommandManager)) as CommandManager;
           //cmdmanager?.Commands.Add(dwcmd);
            var cbm = serviceManager.GetService(typeof(CommandBarManager)) as CommandBarManager;
            cbm.AllowCustomization = true;
            cbm.BeginUpdate();
            var cbar = cbm.CommandBars.AddCommandBar(typeof(结构工具).Name);//typeof(结构工具).Name
            cbar.DockedPosition = DockedPosition.Top;
            cbar.AllowHiding = false;
            cbar.AllowFloating = true;
            var assemblyDate = System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy年MM月dd日-HH点mm分");
            cbar.Caption = cbar.Key + $",软件版本({assemblyDate})";

            var cur002 = cbm.RootTools.AddButtonTool(nameof(结构工具), nameof(结构工具), null, dwcmd);
            var btl2 = cbar.Tools.AddTool(nameof(结构工具)) as ButtonTool;
            cbm.EndUpdate(true);
            cbm.SaveLayout();

        }

        public void Stop()
        {
        }
        public static Dictionary<string, Dictionary<string,MethodInfo>> GetAllCommand()
        {
            Dictionary<string, Dictionary<string, MethodInfo>> dicts = new Dictionary<string, Dictionary<string, MethodInfo>>();
            List<Type> allClass = Assembly.GetExecutingAssembly().GetTypes().Where(a => a.IsClass && a.IsPublic).ToList<Type>();
            List<MyAMFunction> mis = new List<MyAMFunction>();
            foreach (Type item in allClass)
            {
                List<MethodInfo> curClsMis = item.GetMethods().Where(m => m.GetCustomAttributes(typeof(MyAmFunctionAtt), true).Any()).ToList<MethodInfo>();
                if (curClsMis.Count > 0)
                {
                    foreach (MethodInfo mi in curClsMis)
                    {
                        mis.Add(new MyAMFunction(mi));
                    }
                }
            }

            Dictionary<string, Dictionary<string, MethodInfo>> GetAllCommand;
            if (mis.Count == 0)
                GetAllCommand = dicts;
            else
            {
                foreach (MyAMFunction item2 in mis)
                {
                    Dictionary<string, MethodInfo> temp = new Dictionary<string, MethodInfo>();
                    if (dicts.ContainsKey(item2.Att.MenuName))
                    {
                        temp = dicts[item2.Att.MenuName];
                    }
                    temp.Add(item2.Att.FunctionName, item2.Method);
                    dicts[item2.Att.MenuName] = temp;
                }
                GetAllCommand = dicts;
            }
            return GetAllCommand;
        }
    }
    public class MyAMFunction
    {
        public MyAmFunctionAtt Att { get; set; }
        public MethodInfo Method { get; set; }
        public MyAMFunction(MethodInfo mi)
        {
            this.Att = (MyAmFunctionAtt)mi.GetCustomAttributes(true).First(a => a.GetType().FullName == typeof(MyAmFunctionAtt).FullName);
            this.Method = mi;
        }
    }

    public class MyAmFunctionAtt : Attribute
    {
        public string MenuName { get; set; }
        public string FunctionName { get; set; }
        public MyAmFunctionAtt(string _menuNam, string _functionName)
        {
            this.MenuName = _menuNam;
            this.FunctionName = _functionName;
        }
    }


    public class 结构工具 : Command
    {
        private DockedWindow _mywin;

        public 结构工具(WindowManager wm)
        {
            base.Key = this.GetType().FullName;
            var con = new UserControl1(wm, this);
            AmCommand cmds = new AmCommand
            {
                Dict = MianClass.GetAllCommand()
            };
            con.DataContext = cmds;
            var eh = new ElementHost()
            {
                Child = con,
                AutoSize = true,
                Dock = DockStyle.Fill
            };
            _mywin = wm.CreateDockedWindow("xxxx", $"xxxx", eh, DockedPosition.Right);
            _mywin.MaximumSize = new System.Drawing.Size(185, 850);
            _mywin.MinimumSize = new System.Drawing.Size(135, 850);
            _mywin.DockGroupStyle = DockGroupStyle.TabGroup;
            _mywin.SaveLayout = true;
            wm.WindowLayoutLoaded += (s, e) => this.Checked = false;
            _mywin.Closed += (s, e) => this.Checked = _mywin.Visible;
            this.ExecuteOnCheckedChange = false;
        }

        public class AmCommand
        {
            public Dictionary<string, Dictionary<string, MethodInfo>> Dict { get; set; } = new Dictionary<string, Dictionary<string, MethodInfo>>();
            public AmCommand() { }
        }
        public override void Execute()
        {
            try
            {
                if (_mywin.Visible) _mywin.Hide(); else _mywin.Show();
                base.Execute();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.StackTrace);
            }
        }
    }
}

 

posted @ 2023-10-28 19:07  南胜NanSheng  阅读(325)  评论(0编辑  收藏  举报