Partager via


IXRButton (Compact 2013)

3/28/2014

This class represents a button control, which responds to the Click event raised by the inherited class IXRButtonBase.

Syntax

class IXRButton : public IXRButtonBase

Inheritance Hierarchy

IXRDependencyObject

    IXRUIElement

        IXRFrameworkElement

            IXRControl

                IXRContentControl

                    IXRButtonBase

                        IXRButton

Methods

This class implements no new methods. All its methods are inherited from IXRButtonBase.

Thread Safety

Members of this class are thread-safe if you previously called IXRApplication::CreateHostFromXaml and supplied it with an XRWindowCreateParams structure that has AllowsMultipleThreadAccess set to true.

Remarks

The IXRButton class inherits directly from the IXRButtonBase class.

Use the inherited method IXRButtonBase::AddClickEventHandler to add a delegate to respond when the user clicks this button control. You can change how this button control raises the Click event by using the IXRButtonBase::SetClickMode method.

When you create a class instance, use an IXRButtonPtr smart pointer instead of a raw interface pointer. For more information, see XRPtr<Interface>.

You can also define a button control in Microsoft Silverlight 3 XAML. For information about the differences between XAML in XAML for Windows Embedded and Silverlight 3, see Differences Between Microsoft Silverlight 3 and XAML for Windows Embedded. For more information about how to define this element in the source XAML for your application, see the Button Class on MSDN.

Note

You should use the Click event to detect a button click instead of using the MouseLeftButtonDown or MouseLeftButtonUp events.

Example

The following code example shows how to create a button control, set properties on it, attach delegates to it, and add it to the content of an item in the element tree.

Note

To make the following code example easier to read, security checking and error handling are not included. This code sample should not be used in a release configuration unless it has been modified to include them.

#include <windows.h>
#include <XamlRuntime.h>
#include <XRDelegate.h>
#include <XRPtr.h>



void AddButtonToExistingElementTree(IXRApplication* pApplicationObject, 
IXRVisualHost* pVisualHost, IXRSolidColorBrush* pMyBlueBrush, 
EventHandler* pEventHandler)
{
    IXRButtonPtr                pButton;
    IXRTextBlockPtr             pTextBlock;
    IXRFrameworkElementPtr      pRootElement;
    IXRStackPanelPtr            pStackPanel;
    IXRUIElementCollectionPtr   pChildrenCollection;

    // Create a new button control from the IXRApplication object
    // using the CreateObject template helper for type safety
    //
    pApplicationObject->CreateObject(&pButton);
    pApplicationObject->CreateObject(&pTextBlock);

    // Here's where you would set the properties you want on the button 
    // control
    // (for example, width, height, fill, border, etc)
    // 
    pButton->SetWidth(10);
    pButton->SetHeight(5);
    pButton->SetBackground(pMyBlueBrush);
    
    // Set the text for the text block, and add it as the content
    // of the button   
    //
     WCHAR* text = L"Sync";
     pTextBlock->SetText(text);
     pTextBlock->SetFontSize(8);

     XRValue xrvalue;
     xrvalue.pObjectVal = pTextBlock;
     pButton->SetContent(&xrvalue);


     // Attach delegates to represent custom event handlers

     pButton->AddClickEventHandler(CreateDelegate(&pEventHandler, &EventHandler::OnButtonClick));
     pButton->AddMouseEnterEventHandler(CreateDelegate(&pEventHandler, &EventHandler::OnMouseEnter));
     pButton->AddMouseMoveEventHandler(CreateDelegate(&pEventHandler, &EventHandler::OnMouseMove));
     pButton->AddMouseLeaveEventHandler(CreateDelegate(pEventHandler, &EventHandler::OnMouseLeave));

     pButton->AddGotFocusEventHandler(CreateDelegate(&pEventHandler, &EventHandler::OnGainFocus));
     pButton->AddLostFocusEventHandler(CreateDelegate(&pEventHandler, &EventHandler::OnLostFocus));

    // Now get the root element (as stack panel) from your visual host
    // and add the button as a 
    // child in the panel's collection
    //

    // First, find the stack panel named "MyStackPanel"
    //
    pVisualHost->GetRootElement(&pRootElement);
    pRootElement->FindName(TEXT("MyStackPanel"), &pStackPanel);

    // Now get its children collection and add the new button to it
    //
    pStackPanel->GetChildren(&pChildrenCollection);
    pChildrenCollection->Add(pButton, NULL);
}

The previous code example assumes that you have already created an application instance, parsed the XAML markup into an element tree, and obtained a pointer (pVisualHost) to the visual host. For more information on the programming elements used in this code example, see IXRApplication, IXRVisualHost, XRPtr<Interface>, IXRStackPanel, IXRTextBlock, XRValue, CreateDelegate(class,class::method), and IXRUIElementCollection.

.NET Framework Equivalent

System.Windows.Controls.Button

Requirements

Header

XamlRuntime.h

sysgen

SYSGEN_XAML_RUNTIME

See Also

Reference

Classes for UI Element Management