Here is a trick you could use.
- Make sure your Silverlight Views and View Models are isolated within their own assembly that is easily referenceable by your WPF application.
- Add a reference to the Silverlight class library that houses the Views & View Models in the WPF application.
- Move the contents of the UserControl, “CustomerView” into a DataTemplate housed in a resource dictionary called “customerViewTemplate”
- Inside your root UI element XAML files in Silverlight and WPF do this:
<ContentControl ContentTemplate="{Staticresource customerViewTemplate}" />
- In the Silverlight application’s App.xaml make sure to add the following Resource Dictionary reference to the merged dictionaries.
<ResourceDictionary Source="MyApp.Views;component/CustomerViewResources.xaml" />
- In the WPF application’s App.xaml make sure to add the following resource dictionary reference to the merged dictionaries.
<ResourceDictionary Source="pack://application:,,,/MyApp.Views;component/CustomerViewResources.xaml" />
Sorry about the numbering, looks like Stack Overflow’s ordered list mechanism is a little off.
The reason why this works is because you can’t directly reference a Silverlight UserControl from XAML within WPF. It will give you the following error:
‘Cannot resolve dependency to assembly ‘System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’ because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.
If you try to force the UserControl onto a WPF Grid using C# you will get the following 3 errors:
The best overloaded method match for ‘System.Windows.Controls.UIElementCollection.Add(System.Windows.UIElement)’ has some invalid arguments.
The type ‘System.Windows.Controls.UserControl’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’
cannot convert from ‘ToWpfTest.Views.TestView’ to ‘System.Windows.UIElement’
I gather this is because the System.Windows.UIElement in WPF is not the same as the System.Windows.UIElement in Silverlight.
Here is a screen shot of the Silverlight Class Library being referenced directly by the WPF application.
Here is a sample solution.
Nice, thanks!
LikeLike
Hi Markti, I just downloaded and used your example from the blog. Thanks. But in the latest version of Visual Studio, after opening and converting the project, your users need to do the following: 1. In project ToWpfTest, remove references to System.ComponentModel and System.ComponentModel.DataAnnotations, and then 2. Rebuild, 3. Re-add a reference to System.ComponentModel.DataAnnotations. Hope that helps!
LikeLike
Then, when I upgraded it to use Silverlight 4, to the ToWpfTest project I had to add references to System.ServiceModel, System.ServiceModel.DomainServices.Client, and System.ServiceModel.DomainServices.Client.Web
LikeLike
Then, after setting ToWPFTest.Web as startup project, I had to go into the MainPage.xaml file in the ToWpfTest project and add a forward slash to the beginning of the Source element in line 10, so that it became:
LikeLike
Can you please create a working sample in silverlight 4.0
LikeLike