locked
Silverlight and WPF Custom Controls RRS feed

  • Question

  • Is it possible to create custom or user controls or class libraries that can be shared between Silverlight and WPF?   I have made some SL controls and wanted to know the process of rapidly deploying them to WPF.  Most of the code can be ported over to WPF but it would be nice to have a class library to centralize the controls or logic.

    Tuesday, December 8, 2009 11:55 AM

Answers

All replies

  • It can be done but generally you need seperate xaml for each implementation.

    The code behind can be quite easily shared by using compiler directives.

    We've done this easily but we're using mvvm to achieve this based on some of the examples that come with Prism.

    Tuesday, December 8, 2009 11:59 AM
  • Thanks for the quick reply. 

    That would make sense that the code behind could be shared.  For this purpose, is it recommend to push more for Custom or User Controls, or would both be able to port over?

    Are there any online resources or tutorials to perform this task?  You mentioned MVVM and Prism.

    I have a Visual Studio project that has a Silverlight class library, Silverlight output, and WPF output.  Previously, I would copy and paste the XAML and codebehind to both projects with very minor changes to the WPF one.  It would be nice to centralize as much as possible.

    Tuesday, December 8, 2009 12:48 PM
  • Hi FuryDiamond,

    Regarding Prism 2 examples & resources you can check the following link on
    Composite Application Guidance for WPF and Silverlight - October 2009

    http://www.microsoft.com/downloads/details.aspx?FamilyID=387c7a59-b217-4318-ad1b-cbc2ea453f40&displaylang=en

    Online video:
    http://channel9.msdn.com/shows/Continuum/Prismv2/

    If this answers your question, please select Mark as an answer. Thank you! 

    Tuesday, December 8, 2009 2:18 PM
  • Thanks for the quick reply. 

    That would make sense that the code behind could be shared.  For this purpose, is it recommend to push more for Custom or User Controls, or would both be able to port over?

    Are there any online resources or tutorials to perform this task?  You mentioned MVVM and Prism.

    I have a Visual Studio project that has a Silverlight class library, Silverlight output, and WPF output.  Previously, I would copy and paste the XAML and codebehind to both projects with very minor changes to the WPF one.  It would be nice to centralize as much as possible.

    If you install the latest CAL / Prism it has examples of multi-targetted code.

    http://www.codeplex.com/CompositeWPF

    The standard practice is to have wpf xaml and sl xaml seperated. The code behind (or module/viewmodel) can be shared between the 2 projects by linking the file into the project. The compiler directives handles the rest.

    As to whether custom or usercontrol that always comes down to how you need to re-use them. If it's just within your app then custom controls are more work. If it's something that needs to be easily retemplated by other developers then custom would make sense. It's really a case by case basis.

    Tuesday, December 8, 2009 3:59 PM