AddClickEventHandler(CreateDelegate(pEventHandler, &EventHandler::OnButtonClick)); … class EventHandler { public: HRESULT OnButtonClick(IAXDependencyObject* pSender, AXEventArgs* pArgs) { // Do something when the user clicks the button… }"> AddClickEventHandler(CreateDelegate(pEventHandler, &EventHandler::OnButtonClick)); … class EventHandler { public: HRESULT OnButtonClick(IAXDependencyObject* pSender, AXEventArgs* pArgs) { // Do something when the user clicks the button… }">
Download presentation
Presentation is loading. Please wait.
Published byWillis Wilkinson Modified over 8 years ago
1
Andy Wigley Device Application Development MVP APPA Mundi Ltd SESSION CODE: WEM309
6
private void Button1_Click(object sender, RoutedEventArgs e) { // Turn screen background red this.LayoutRoot.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0)); }
7
// Locate the button in the object tree IXRButtonPtr pButton; pVisualHost->GetRootElement(&pRootElement); FindName(pRootElement, L"Button1", IID_IAXButton, &pButton); // Add OnClick Event handler pButton->AddClickEventHandler(CreateDelegate(pEventHandler, &EventHandler::OnButtonClick)); … class EventHandler { public: HRESULT OnButtonClick(IAXDependencyObject* pSender, AXEventArgs* pArgs) { // Do something when the user clicks the button… }
8
C:\Projects\SWETest> xaml2cpp Page.xamlC:\Projects\SWETest> xaml2cpp Page.xaml XAML2CPP version 1.0.2.0XAML2CPP version 1.0.2.0C:\Projects\SWETest>
13
// ========================================================================== // WinMain – Application control logic for the native SWE app // ========================================================================== INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE /*hInstPrev*/, LPWSTR /*lpCmdLine*/, int /*nShowCmd*/) { App AppInstance; HRESULT hr = AppInstance.Initialize(hInstance); if(SUCCEEDED(hr)) { hr = AppInstance.Run(); } return AppInstance.GetWinMainResultCode(); }
14
HRESULT MainPage::OnLoaded(__in IXRDependencyObject* pRoot) { UNREFERENCED_PARAMETER(pRoot); HRESULT hr = InitializeComponent(); // Add calls to FindName or Add___EventHandler() methods after this comment. if (m_pMyButton) { m_pMyButton->AddClickEventHandler( CreateDelegate(this, &MainPage::OnButtonClick)); } return hr; } // OnLoaded
15
// =========================================================================== // OnButtonClick // Description: Event handler implementation // // Parameters: pSender - The dependency object that raised the click event. // pArgs - Event specific arguments. // =========================================================================== HRESULT MainPage::OnButtonClick (IXRDependencyObject* pSender, XRMouseButtonEventArgs* pArgs) { HRESULT hr = E_NOTIMPL; if ((NULL == pSender) || (NULL == pArgs)) { hr = E_INVALIDARG; } return hr; }
17
Native DLL Managed EXE Page Click Me Textbox
19
#ifdef SWENATIVE_EXPORTS #define SWENATIVE_API __declspec(dllexport) #else #define SWENATIVE_API __declspec(dllimport) #endif extern "C" SWENATIVE_API int RunXamlUI(); extern "C" SWENATIVE_API int GetControlText(LPWSTR lName, BSTR* pValue); extern "C" SWENATIVE_API int SetControlText(LPWSTR lName, LPWSTR lValue); extern "C" SWENATIVE_API int StartStoryboard(LPWSTR lName);
20
HINSTANCE hDll; static App *app; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: hDll = (HINSTANCE)hModule; // Save the HINSTANCE handle of the dll break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern "C" SWENATIVE_API int RunXamlUI() // Start the XAML UI { app = new App(); if (FAILED(app->Initialize(hDll))) return -1; // Call Initialize passing HINSTANCE of the dll if (FAILED(app->Run())) return -1; // Run doesn’t return until XAML UI exits return 0; }
21
using System.Runtime.InteropServices; namespace ManagedConsoleHost { class Program { [DllImport("SWEnative.dll")] static extern int RunXamlUI(); static void Main(string[] args) { // Call the native method to run the UI RunXamlUI(); }
22
// This in SWEnative.h. Callback function prototype typedef void (CALLBACK *CLICKCALLBACK)(LPWSTR lParam); // Modify the RunXamlUI function to accept a parameter that specifies the callback extern "C" SWENATIVE_API int RunXamlUI(CLICKCALLBACK callback); extern "C" SWENATIVE_API int RunXamlUI(CLICKCALLBACK callback) { app = new App(); app->ClickCallback = callback; // Save the callback function for later …
23
// This in MainPage.cpp: Declare event handler in UI class // ============================================================================ // OnButtonClick // // Description: Event handler implementation // Parameters: pSender - The dependency object that raised the click event. // pArgs - Event specific arguments. // ============================================================================ HRESULT MainPage::OnButtonClick (IXRDependencyObject* pSender, XRMouseButtonEventArgs* pArgs) { if (App::ClickCallback != NULL) { BSTR controlName; pSender->GetName(&controlName); // Call the callback function, passing the name of the control raising the event App::ClickCallback((LPWSTR)controlName); } return S_OK; }
24
using System.Runtime.InteropServices; namespace SWEManagedHost { class Program { [DllImport("SWEnative.dll")] static extern int RunXamlUI(ClickCallBackDelegate cbdele); // Declare a delegate that matches the signature of the native CALLBACK public delegate void ClickCallBackDelegate(StringBuilder msg); // Make this a static - to make sure // that the function pointer doesn't get garbage collected later on static ClickCallBackDelegate cbdele; // Continued…
25
static void Main(string[] args) { // Declare the delegate to the callback function cbdele = new ClickCallBackDelegate(SilverlightClickEventHandler); // Call the native method to start up the UI, passing the delegate RunXamlUI(cbdele); } // Event handler executes when called from native private void SilverlightClickEventHandler(StringBuilder msg) { MessageBox.Show("Click from Silverlight, control name: " + msg.ToString); }
27
Native DLL Managed EXE Page Textbox Click Me Get Start/Stop Set
28
Model – View – Presenter design pattern View1View2 Presenter logic Model - data Interface
29
SLNative.lib
30
Control View Button TextBox Others… View Framework App code Application logic SLNative.lib Native Managed
31
public class SampleApplicationView : SLBaseView { private Appamundi.Windows.Controls.Button helloButton = null; private Appamundi.Windows.Controls.TextBox txtForename = null; private Appamundi.Windows.Controls.TextBox txtSurname = null; public SampleApplicationView(BaseShell shell):base(shell) { InitializeView(); // Create control objects that are proxies for those created in XAML helloButton = new Appamundi.Windows.Controls.Button("btnSayHello", this); txtForename = new Appamundi.Windows.Controls.TextBox("txtForename", this); txtSurname = new Appamundi.Windows.Controls.TextBox("txtSurname", this); helloButton.Click += new EventHandler( (s, a) => { txtForename.Text = "Vladimir"; // Set a control property MessageBox.Show("Hello " + txtForename.Text + " " + txtSurname.Text); }); }
34
<UserControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:xr="clr-namespace:EmbeddedXamlRuntime" x:Class="SimpleSLapp.Page" Width="640" Height="480" Background="#FFC42929"> <xr:Win32Control x:Name="WinFormTest" Height="200" Width="200" HorizontalAlignment="Left" Margin="203,0,237,23“ VerticalAlignment="Bottom" />
35
int ShowWin32Control(HWND win32Handle) { HRESULT retcode; IXRWin32ControlPtr pIWin32Ctl; // Find the placeholder IXRWin32Control control in the Silverlight object tree if (FAILED(retcode=root->FindName(L"WinFormTest",&pIWin32Ctl))) return retcode; // Swap the handle of the found control with the one of the Win32 control pIWin32Ctl->SetHandle(win32Handle, false); // The Win32 control is now represented in the visual tree // and is integrated with the on-screen layout of peer controls in the window. // activate the visual host window and repaint the changed portion HWND HostWindow = NULL; GetVisualHost()->GetContainerHWND(&HostWindow); ShowWindow(HostWindow, SW_SHOW); UpdateWindow(HostWindow); return 0; }
36
public partial class Form1 : Form { [DllImport("SWEnative.dll")] static extern int ShowWinFormControl(IntPtr handle);... private void SilverlightClickEventHandler(StringBuilder msg) { // Create an instance of the winforms user control we want to // plug into the Silverlight display UserControl1 uc = new UserControl1(); uc.Visible = true; // Tell the Silverlight UI about it and it will display it ShowWinFormControl(uc.Handle); }
39
Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub.
42
www.microsoft.com/teched www.microsoft.com/learning http://microsoft.com/technet http://microsoft.com/msdn
44
Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31 st http://northamerica.msteched.com/registration You can also register at the North America 2011 kiosk located at registration Join us in Atlanta next year
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.