Host a Silverlight application in Winforms application - winforms

I have a need to host a Silverlight application as a module in an existing WinForms application. My plan is to host the web page that hosts the Silverlight app in a web browser control in the WinForms application.
The part I have not been able to figure out is how to communicate from the Silverlight application back to the Winforms application. For starters, the Silverlight application needs to inform the web page that it is closing and have the browser control inform the WinForms module that the application has terminated. Once that is working I need to be able to send several different events to the WinForms program.
This does not seems like it should be hard to do, but I am hoping that someone can save me a lot of experimenting and keep me from wasting time following the wrong path.
My environment is Silverlight 4. The Winforms application also currently is hosting some WPF controls.

kind of a backwards approach.
Silverlight is a workaround to make WPF available on other platforms.
If you are running winforms, just host it as WPF window. It is really easy.
MyWindow window = new MyWindow(); // WPF widnow
ElementHost.EnableModelessKeyboardInterop(window) // this is part of windforms integration namespace, allows winforms and wpf to communicate
window.Show();

Related

Steps for change the UI of desktop app from winforms to wpf

I built a small desktop app on c# .net
Local desktop without any internet connection or communication with other any computers, I used winforms.
As expected I split up between logic and UI, and now I want to use WPF to my UI (change the way I implement the UI part), and in the future I will want to use mobile UI..
What are my steps should be?
Do I need to write new code, or there is something automatic?

Can we integrate a WinForms application with a WPF application?

I want to integrate two existing applications into one. One of those apps is built on Windows Forms and the other on WPF.
Is it possible to achieve this?
WPF supplies the WindowsFormsHost class that allows you to host WinForms controls inside a WPF window; conversely, WinForms supplies ElementHost that allows you to host WPF controls inside a form.
Unfortunately how well things work out is highly dependent on exactly what you are doing, last time I checked there were more than a few rough edges. For more information, definitely start from this MSDN page.
If you want to have "independent" WPF windows and WinForms forms inside the same application, you will have to make both frameworks "share" some code in your UI thread's message loop. For a primer on how to do that, see here.
There are various classes to help you with this.
For hosting Windows Forms controls in a WPF Window, you can use the WindowsFormsHost class. For hosting WPF Controls in a Windows Forms Window, you can use the ElementHost class.
You can look here for mor information on the subject (windows forms section):
http://msdn.microsoft.com/en-us/library/ms753178.aspx

Event Aggregration between WPF and Silverlight

I have one silverlight application hosted in WPF environment.
i have created one static eventaggregrator class.
which is used between multiple modules hosted using prism.
but for the event which need to be publish from a module developed in WPF and subscribe in the module developed in *silverlight*.
is it possible? if yes then can anybody give me some ref article or solutions?
WPF and Silverlight use different execution environments, so code cannot be shared at runtime.
If you are hosting the Silverlight control inside a WebBrowser control in WPF then you will have to insert a Javascript layer inbetween your Silverlight control and the WPF host.
So you will subscribe in the .NET world, and then use the InvokeScript() method on your WebBrowser control to invoke a Javascript script which can communicate with the Silverlight control (see ScriptableMember).

Can I replace WPF by Silverlight

Could Silverlight and WPF be interchangeable?
I mean, using Silverlight for Windows applications and WPF for web applications.
Silverlight and WPF are similar, but not interchangeable.
Silverlight and WPF are two different technologies intended to solve different problems. Silverlight has out-of-browser functionality, but must be delivered over the web in a browser first. WPF has XBAP, but it's still a WPF application. They are both XAML-based, so there is some limited ability to share resources.
If you are designing an application, select the technology that is most appropriate to the problem and the target audience. Keep in mind that while Silverlight runtime installs are growing fast, they still lag far behind others. Some people flatly refuse to install it. WPF apps can be distributed like desktop applications with an executable.
Update: In my experience, WPF XBAP applications don't behave well in browsers that aren't IE.
SL 4 can work in an out of browser mode. WPF has the XBAP (XAML browser application). So in a way they are but consider the limitations you may encounter in your projects.
Regards...
Some user controls can be reused between the two platforms. But because of the very different ways Silverlight and WPF interact with their environments (WPF on local system, Silverlight in browser plug-in sandbox) not all of the code is fully interchangable.
It's theoretically possible, but would require extra effort to port. And each is best suited to its environment. Here's an interesting discussion from an MS forum:
http://forums.silverlight.net/forums/p/1178/4244.aspx
For the most part, WPF is designed to run on the desktop and Silverlight and designed to run as part of a web page.
You can deploy a silverlight application to a desktop and run it from there, but there is no way to run a WPF UI "on the web".
With Silverlight 4 you can create a full trust application that is installed in the same was as a WPF application.
This article has a walkthrough.
So in this sense you can replace WPF with Silverlight.
You can link to WPF XAML pages on the web. This page has such a link, but you need a plugin to view it. I wouldn't want to deploy a full application this way though & I think it's only individual pages.

How to detect whether code is running in a XBAP application or a WPF application?

Im creating a custom control. I have common code for WPF and XBAP application. I have some different calculation/work to do if it’s a XBAP application. How to detect whether it’s an xbap application or a WPF application?
You can use System.Windows.Interop.BrowserInteropHelper.IsBrowserHosted property identify whether it is hosted on web or windows

Resources