Difference between revisions of "Inserting Delegate Stubs in VS2012 (CS)"

From Ribbon Commander Documentation
Jump to: navigation, search
(Example)
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
 
Consider the simple example below:
 
Consider the simple example below:
 
<syntaxhighlight lang="csharp" line>
 
<syntaxhighlight lang="csharp" line>
 +
class SubscribeToDelegates
 +
{
 +
    rxCustomUI _customUI;
 +
 +
    public SubscribeToDelegates()
 +
    {
 +
        // Create a new customUI obj
 +
        _customUI = rxCustomUI.create("my_test_ctx");
 +
 +
        // Create a new button
 +
        rxButton myButton = new rxButton();
 +
 +
        // Create a new delegate of the correct signature
 +
        // NOTE1: We use class rxButtonDelegates to conveniently get the desired delegate type
 +
        // NOTE2: The delegate stub hasn't been provided at this point
 +
        var myDel = new rxButtonDelegates.onAction(MyOnAction);
 +
 +
        // Subscribe to the button's onAction delegate
 +
        myButton.onAction = _customUI.make_delegate(myDel);
 +
           
 +
    }
 +
}
 +
</syntaxhighlight>
 +
* Note1: At this point we haven't yet provided the delegate stub, so intellisense marks ''myOnAction'' with red squiggles:
 +
*: [[image: MyOnActionRedSquiggles.png|border|link=]]
 +
* Note2: When clicking inside ''MyOnAction'', intellisense provides us with the option to insert the delegate stub automatically:
 +
*: [[image: InsertDelegateStubCS.png|border|link=]]
 +
 +
The class now becomes:
 +
<syntaxhighlight lang="csharp" line>
 +
class SubscribeToDelegates
 +
{
 +
    rxCustomUI _customUI;
 +
 +
    public SubscribeToDelegates()
 +
    {
 +
        // Create a new customUI obj
 +
        _customUI = rxCustomUI.create("my_test_ctx");
 +
 +
        // Create a new button
 +
        rxButton myButton = new rxButton();
 +
 +
        // Create a new delegate of the correct signature
 +
        // NOTE1: We use class rxButtonDelegates to conveniently get the desired delegate type
 +
        // NOTE2: The delegate stub hasn't been provided at this point
 +
        var myDel = new rxButtonDelegates.onAction(MyOnAction);
 +
 +
        // Subscribe to the button's onAction delegate
 +
        myButton.onAction = _customUI.make_delegate(myDel);
 +
           
 +
    }
 +
 +
    private void MyOnAction(IRibbonControl control)
 +
    {
 +
        throw new NotImplementedException();
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 17:07, 15 March 2013

Introduction

In Visual Studio 2012, you can automatically insert delegate stubs based on their signature in C#.

Example

Consider the simple example below:

  1. class SubscribeToDelegates
  2. {
  3.     rxCustomUI _customUI;
  4.  
  5.     public SubscribeToDelegates()
  6.     {
  7.         // Create a new customUI obj
  8.         _customUI = rxCustomUI.create("my_test_ctx");
  9.  
  10.         // Create a new button
  11.         rxButton myButton = new rxButton();
  12.  
  13.         // Create a new delegate of the correct signature
  14.         // NOTE1: We use class rxButtonDelegates to conveniently get the desired delegate type
  15.         // NOTE2: The delegate stub hasn't been provided at this point
  16.         var myDel = new rxButtonDelegates.onAction(MyOnAction);
  17.  
  18.         // Subscribe to the button's onAction delegate
  19.         myButton.onAction = _customUI.make_delegate(myDel);
  20.  
  21.     }
  22. }
  • Note1: At this point we haven't yet provided the delegate stub, so intellisense marks myOnAction with red squiggles:
    MyOnActionRedSquiggles.png
  • Note2: When clicking inside MyOnAction, intellisense provides us with the option to insert the delegate stub automatically:
    InsertDelegateStubCS.png

The class now becomes:

  1. class SubscribeToDelegates
  2. {
  3.     rxCustomUI _customUI;
  4.  
  5.     public SubscribeToDelegates()
  6.     {
  7.         // Create a new customUI obj
  8.         _customUI = rxCustomUI.create("my_test_ctx");
  9.  
  10.         // Create a new button
  11.         rxButton myButton = new rxButton();
  12.  
  13.         // Create a new delegate of the correct signature
  14.         // NOTE1: We use class rxButtonDelegates to conveniently get the desired delegate type
  15.         // NOTE2: The delegate stub hasn't been provided at this point
  16.         var myDel = new rxButtonDelegates.onAction(MyOnAction);
  17.  
  18.         // Subscribe to the button's onAction delegate
  19.         myButton.onAction = _customUI.make_delegate(myDel);
  20.  
  21.     }
  22.  
  23.     private void MyOnAction(IRibbonControl control)
  24.     {
  25.         throw new NotImplementedException();
  26.     }
  27. }