ERROR "Operation could destabilize the runtime." shows when trying to open menu

3 posts, 0 answers
  1. Todor
    Todor avatar
    10 posts
    Member since:
    Aug 2008

    Posted 10 Feb 2009 Link to this post

    Hello guys,

    I am writing a Custom Control and I use your ContextMenu. In that control I have 2 buttons. Button 1 has a ContextMenu attached. I want to show the ContextMenu under Button 1 when I click Button 2, so I created a custom event and a public method to Button 1, that looks like this:

    public delegate void ShowContextMenuHandler(object sender); 
    public event ShowContextMenuHandler ShowContextMenuEvent; 
     
    protected void OnShowContextMenu(object sender) 
        if (ShowContextMenuEvent != null
        { 
            ShowContextMenuEvent(sender); 
        } 
    }  
     
    public void ShowContextMenu() 
        OnShowContextMenu(this); 
    }  

    In the .cs file of the Custom Control I handle the Click Event of Button 2:

    btnMenu.Click += new RoutedEventHandler(btnMenu_Click); 
     
    void btnMenu_Click(object sender, RoutedEventArgs e) 
        btnMain.ShowContextMenu(); 
    }  

    And the declaration of the menu looks like this:
    <my:ButtonLeft Height="30" 
               Width="91" 
               Content="Save" 
               x:Name="btnMain"
        <telerik:RadContextMenu.ContextMenu> 
            <telerik:RadContextMenu x:Name="DropDownContextMenu"  
                        EventName="ShowContextMenuEvent" 
                        Placement="Bottom"
                <telerik:RadMenuItem Header="Save"/> 
                <telerik:RadMenuItem Header="Save as..."/> 
                <telerik:RadMenuItem IsEnabled="False" Header="Don't Save"/> 
            </telerik:RadContextMenu> 
        </telerik:RadContextMenu.ContextMenu> 
    </my:ButtonLeft> 

    But when I click Button 2, I get this exception:

    "An exception of type 'System.Security.VerificationException' occurred in DynamicTypes but was not handled in user code
    Additional information: Operation could destabilize the runtime."

    And it is pointing to my "ShowContextMenuEvent(sender);" line in the "OnShowContextMenu(object sender)" method.

    Any idea what am I doing wrong ? It it is a bug into the ContextMenu ?
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 12 Feb 2009 Link to this post

    Hi Todor,

    RadContextMenu uses reflection in order to attach handler to the event that will trigger menu opening. So we are expecting that the handler will have two parameters - sender and event args.
    In order to run your code you should change the event handler for the ShowContextMenuEvent to match the standard event handler signature:

    public class ButtonLeft : Button  
    {  
        public delegate void ShowContextMenuHandler(object sender, EventArgs e);  
        public event ShowContextMenuHandler ShowContextMenuEvent;  
     
        protected void OnShowContextMenu(object sender)  
        {  
            if (ShowContextMenuEvent != null)  
            {  
                ShowContextMenuEvent(sender, EventArgs.Empty);  
            }  
        }  
     
        public void ShowContextMenu()  
        {  
            OnShowContextMenu(this);  
        }  

    Let me know if you have other problems.

    Greetings,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Todor
    Todor avatar
    10 posts
    Member since:
    Aug 2008

    Posted 12 Feb 2009 Link to this post

    Hi Hristo,
    I thought that this might be the case, but I wasn't sure of the default structure you use. Thanks, I will try it and report if I have any other problems. Keep up the good work !
Back to Top