代码创建的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 } } }