代码创建的ribbon在切换工作空间后消失的解决方法
autodesk论坛中有现成的解决方法
标题
RibbonTab in all Workspaces
链接 https://forums.autodesk.com/t5/net/ribbontab-in-all-workspaces/td-p/2853958
主要回帖内容:
When you change workspace your ribbon wilI be unloaded. Listen to the SystemVariableChanged event and watch the WSCURRENT setting. This system variable will be changed when a new workspace become current. If this is changed you can run your code to add your ribbon tab.
Here is an example
public void Initialize()
{
if (Autodesk.Windows.ComponentManager.Ribbon == null)
{
//load the custom Ribbon on startup, but at this point
//the Ribbon control is not available, so register for
//an event and wait
Autodesk.Windows.ComponentManager.ItemInitialized +=
new EventHandler(ComponentManager_ItemInitialized);
//not in published sample
Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged
+= new
Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(Application_SystemVariableChanged);
}
else
{
//the assembly was loaded using NETLOAD, so the ribbon
//is available and we just create the ribbon
Your command for creating your ribbon 'createRibbon();'
Autodesk.AutoCAD.ApplicationServices.Application.SystemVariableChanged +=
new Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventHandler(Application_SystemVariableChanged);
}
}
void Application_SystemVariableChanged(object sender,
Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e)
{
//Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//ed.WriteMessage("\n-Sys Var Changed: " + e.Name);
if (e.Name.ToLower() == "wscurrent")
{
createRibbon();
}
}
Sorry about te bad code format but i think this will help.
我根据上面的代码进行了一些修改,
修改后的代码能把ribbon存住。
// (C) Copyright 2019 by // using System; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; using Autodesk.Windows; using System.Drawing; // This line is not mandatory, but improves loading performances [assembly: ExtensionApplication(typeof(RibbonTest.MyPlugin))] namespace RibbonTest { // This class is instantiated by AutoCAD once and kept alive for the // duration of the session. If you don't do any one time initialization // then you should remove this class. public class MyPlugin : IExtensionApplication { void IExtensionApplication.Initialize() { // Add one time initialization here // One common scenario is to setup a callback function here that // unmanaged code can call. // To do this: // 1. Export a function from unmanaged code that takes a function // pointer and stores the passed in value in a global variable. // 2. Call this exported function in this function passing delegate. // 3. When unmanaged code needs the services of this managed module // you simply call acrxLoadApp() and by the time acrxLoadApp // returns global function pointer is initialized to point to // the C# delegate. // For more info see: // http://msdn2.microsoft.com/en-US/library/5zwkzwf4(VS.80).aspx // http://msdn2.microsoft.com/en-us/library/44ey4b32(VS.80).aspx // http://msdn2.microsoft.com/en-US/library/7esfatk4.aspx // as well as some of the existing AutoCAD managed apps. // Initialize your plug-in application here Application.Idle += LoadMyRibbon; Application.SystemVariableChanged += Application_SystemVariableChanged; } static void Application_SystemVariableChanged(object sender, Autodesk.AutoCAD.ApplicationServices.SystemVariableChangedEventArgs e) { //Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; //ed.WriteMessage("\n-Sys Var Changed: " + e.Name); if (e.Name.ToLower() == "wscurrent") { CreateRibbon(); Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nribbon重建完成"); } } static private void LoadMyRibbon(object sender, EventArgs e) { CreateRibbon(); Application.Idle -= LoadMyRibbon; } private static void CreateRibbon() { RibbonControl rc = ComponentManager.Ribbon; var sth = rc.Tabs; RibbonTab rt = null; foreach (RibbonTab tab in rc.Tabs) { if (tab.AutomationName == "Test") { rt = tab; break; } } if (rt == null) { rt = new RibbonTab(); rt.Title = "Test"; rt.Id = "Test_ID"; rc.Tabs.Insert(0, rt); } RibbonPanelSource rps = new RibbonPanelSource(); rps.Title = "第一个"; RibbonPanel rp = new RibbonPanel(); rp.Source = rps; rt.Panels.Insert(0, rp); RibbonButton rb = new RibbonButton(); rb.Text = "第一个按钮"; rb.ShowText = true; rb.ShowImage = true; rps.Items.Add(rb); } void IExtensionApplication.Terminate() { // Do plug-in application clean up here } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?