Can the same control be compiled for both WPF and Silverlight?

I have a complex control for Silverlight, and I need to have the same functionality in WPF. Any way to share the codebase?

+4
source share
2 answers

There are three aspects that will need to be used:

  • Code files (e.g. * .cs * .vb)
  • XAML files (including themes)
  • Project files (such as * .csproj and * .vbproj)

As Yaroslav noted, you can use conditional complication for code files, which is supported by C # and VB.Net . Keep in mind that Silverlight projects will by default define the SILVERLIGHT symbol, so you can use this in your conditional statements.

Another trick for code files is to use partial classes . This allows you to place entire blocks of code that can only be applied to Silverlight or WPF (but not both) in a single file. Then selectively include this file in your project.

Xaml files are a bit more complicated since WPF supports several things that are not supported by Silverlight (like custom MarkupExtensions, etc.). In practice, I simply duplicate XAML files and compress as needed.

Project files must be supported manually, which is not so difficult.

+3
source

I wrote an article about sharing .xaml files. The idea is to create a proxy control in the source code and use this control in .xaml :

 namespace UnifiedXaml { public class MyWrapPanel: WrapPanel { } } 


 <UserControl x:Class="UnifiedXaml.TestControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ux="clr-namespace:UnifiedXaml"> <ux:MyWrapPanel></ux:MyWrapPanel> </UserControl> 

If the control has different functionality in WPF and Silverlight, use styles .

0
source

Source: https://habr.com/ru/post/1343513/


All Articles