How to grab the RibbonPaletteSet at startup?

I've got a .NET app and I'm trying to create a Ribbon for it when the IExtensionApplication.Initialize() event.

The code is set to grab the RibbonPaletteSet in an idle handler, as it's apparently best to wait till the Application settles down after startup before trying to get a hold of the RibbonPaletteSet.

The following code works well in AutoCAD, but when I try to set the RibbonPaletteSet in BricsCAD it returns null.

Should I be trying a different avenue in BricsCAD?

The line in question is in void idle in the RibbonHelper code below:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

#if BRX_APP
using Bricscad.ApplicationServices;
using Bricscad.Windows;
namespace Bricscad.Ribbon
#elif ACAD_APP
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.Windows;
namespace Autodesk.AutoCAD.Ribbon
#endif
{
    /// <summary>
    /// From Tony Tanzillo, found at http://www.theswamp.org/index.php?topic=44440.msg496962#msg496962
    /// 
    /// Pass a delegate that takes the RibbonControl
    /// as its only argument, and it will be invoked
    /// when the RibbonControl is available. 
    /// 
    /// If the RibbonControl exists when the constructor
    /// is called, the delegate will be invoked on the
    /// next idle event. Otherwise, the delegate will be
    /// invoked on the next idle event following the 
    /// creation of the RibbonControl.
    /// </summary>
    public class RibbonHelper
    {
        Action<RibbonControl> _action = null;
        bool idleHandled = false;
        bool created = false;

        RibbonHelper(Action<RibbonControl> action)
        {
            if (action==null)
            {
                throw new ArgumentNullException("initalizer");
            }
            _action = action;
            SetIdle(true);
        }

        public static void OnRibbonFound(Action<RibbonControl> action)
        {
            new RibbonHelper(action);
        }
        private void SetIdle(bool val)
        {
            Debug.WriteLine($"RibbonHelper.SetIdle():val={val}, idleHandled={idleHandled}");
            if (val!=idleHandled)
            {
                if (val)
                {
                    Debug.WriteLine("Adding idlehandler");
                    Application.Idle += idle;
                }
                else
                {
                    Debug.WriteLine("Removing idleHandler");
                    Application.Idle -= idle;
                }
                idleHandled = val;
            }
        }

        private void idle(object sender, EventArgs e)
        {
            SetIdle(false);
            Debug.WriteLine($"idleHandler firing, _action:{_action.ToString()}");
            if (_action!=null)
            {
                RibbonPaletteSet ps = RibbonServices.RibbonPaletteSet;//<-RETURNS NULL IN BRICSCAD

                if (ps!=null)
                {
                    RibbonControl ribbon = ps.RibbonControl;
                    if (ribbon!=null)
                    {
                        _action(ribbon);
                        _action = null;
                    }
                }
                else if (!created)
                {
                    created = true;
                    RibbonServices.RibbonPaletteSetCreated += ribbonPaletteSetCreated;
                }
            }
        }

        private void ribbonPaletteSetCreated(object sender, EventArgs e)
        {
            RibbonServices.RibbonPaletteSetCreated -= ribbonPaletteSetCreated;
            SetIdle(true);
        }
    }
}

Comments

This discussion has been closed.