Things to Do in a Silverlight Application
using System.Windows.Browser;
Use the above mentioned namespace for communication. We will use HTML communication bridge to communicate with the C++ host.
Use the following statement to register the Communicator object. We will get this communicator object in C++ Silverlight host using IDispath.
HtmlPage.RegisterScriptableObject("Communicator", this);
Write another function in Silverlight application which we will call from C++ Silverlight host.
[ScriptableMember]
public void SetDataInTextBox(string strData)
{
txtData.Text = strData;
}
The complete code for the Silverlight page is as follows:
using System.Windows.Browser;
namespace SilverlightTestApp
{
public partial class MainPage : UserControl
{
public MainPage()
{
HtmlPage.RegisterScriptableObject("Communicator", this);
InitializeComponent();
}
private void ClickMe_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button click handler is in Silverlight :)",
"SilverlightTestApp", MessageBoxButton.OK);
}
[ScriptableMember]
public void SetDataInTextBox(string strData)
{
txtData.Text = strData;
}
}
}
I am not trying to explain Silverlight development issues here but I would like to say the same things we do for Silverlight and JavaScript interaction. If someone is not familiar with the above code, Google Silverlight and JavaScript interaction. Almost every Silverlight book covers this topic.
C++ Silverlight Host
Add an edit box and a button in your dialog box in ATL application. We will enter some test in the edit box and write some code in the click handler of the button we just added.
Handler function for button click. Getting the user input in strData...
LRESULT CCMainDlg::OnBnClickedBtnSendDataToSilverlight
(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAtlStringW strData;
GetDlgItem(IDC_EDIT_DATA).GetWindowTextW(strData);
Now coming to the main point - getting DISPID of Communicator object we just registered in Silverlight app.
IDispatch *pContent;
HRESULT hr = CXcpControlHost::GetXcpControlPtr()->get_Content(&pContent);
CComBSTR bstrMember(L"Communicator");
DISPID dispIDCommunicator = NULL;
hr = pContent->GetIDsOfNames(IID_NULL, &bstrMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispIDCommunicator);
if(FAILED(hr))
{
::MessageBox(NULL, L"Failed to get the DISPID for Communicator",
L"Error :(", 0);
return 0;
}
Once we have a valid dispID for Communicator object, we are ready to get the IDispatch* for Communicator object. It's a bit tricky here, we will make an invoke call with DISPATCH_PROPERTYGET.
Read more: Codeproject
0 comments:
Post a Comment