This is a migrated thread and some comments may be shown as answers.

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

2 Answers 113 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Todor
Top achievements
Rank 1
Todor asked on 10 Feb 2009, 01:50 PM
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 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 12 Feb 2009, 08:48 AM
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.
0
Todor
Top achievements
Rank 1
answered on 12 Feb 2009, 09:39 AM
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 !
Tags
Menu
Asked by
Todor
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Todor
Top achievements
Rank 1
Share this question
or