.NET code reuse

3,730 views

Published on

Slide desk from my LA SLUG presentation on May 25, 2011. Sharing code between Silverlight, WPF and WinPhone

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,730
On SlideShare
0
From Embeds
0
Number of Embeds
1,226
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Show ThreePlatforms demo. Start with Silverlight. Then include MapPage.xaml/.cs into WP7 and WPF.
  • Note that SILVERLIGHT conditional also includes Windows Phone.
  • Note that SILVERLIGHT conditional also includes Windows Phone.
  • Note that SILVERLIGHT conditional also includes Windows Phone.
  • Can become a problem with WinPhone, which doesn’t support xmlns namespace mapping that uses a catch-all schema
  • Silverlight can change animations on the fly.WPF: Animations are by default frozen.Begin: True parameter allows for changing animation later (not frozen).Stop: element parameter required when Begin was called with element.
  • SilverlightUserControl ConstructorMyControl ConstructorMyControl LoadedUserControl LoadedMyControl.OnApplyTemplateWPFUserControl ConstructorMyControl ConstructorMyControl.OnApplyTemplateUserControl LoadedMyControl Loaded
  • Assemblies built in Silverlight 2 and 3 are not binary compatible with the .NET Framework, so if you want to share code you need generally need to dual-compile. With Silverlight 4 and .NET 4, you will be able to use some Silverlight-based assemblies from within .NET 4.In order to load a Silverlight assembly in .NET, the assembly may only reference the following assemblies: •Mscorlib•System•System.Core•System.ComponentModel.Composition•Microsoft.VisualBasicOther assemblies such as System.Xml and System.Net are not currently supported, nor are any of the UI libraries. This means sharing passive data objects is possible but active records that can call services or handle their own XML serialization are out of the question.One should also note that loading .NET assemblies in Silverlight is explicitly not supported. Since the non-UI parts of Silverlight are a directly subset of .NET, they felt it was less error prone to do it this way than to try to deal with countless missing classes and methods.If you would like to learn more about assembly portability or want to report an incompatibility between Silverlight and .NET, you can do so on the CLR Team blog.
  • .NET code reuse

    1. 1. a tale from the “real” world<br />.NET Code ReuseWPF | Silverlight | Windows Phone<br />
    2. 2. tonight's topic<br />WPF<br />Silverlight<br />VS<br />
    3. 3.
    4. 4.
    5. 5. agenda<br />Who is this guy?<br />What is he doing?<br />Why is he doing it?<br />How is he doing it?<br />
    6. 6. who is this guy?<br />Morten Nielsen (@sharpgis)<br />Esri – World leader in GIS software<br />Lead Developer<br />ArcGIS API for Silverlight, WPF & WinPhone 7<br />http://esriurl.com/sl<br />Silverlight MVP<br />Danish (sorry for the accent)<br />
    7. 7. what is he doing?<br />ArcGIS API for Silverlight, WPF, WP7<br />Client API for creating advanced GIS analysis on the web<br />Free developer download: http://esriurl.com/slSamples: http://esriurl.com/slsdk<br />
    8. 8. how did he get into this "mess"?<br />It all started with WPF/E…<br />a.k.a Silverlight v1.1alpha… <br />a.k.a. Silverlight v2<br />I wonder if this can compile into WPF?<br />I wonder if this can compile into WinPhone?<br />
    9. 9. advantages<br />Write once!<br />Reuse (WPF), reuse (Silverlight), reuse (Phone), reuse (Xbox?), reuse (???)<br />In other words: Triple your productivity<br />Always syncronizedDataContracts<br />Compile data contracts for both server and client – simple reuse.<br />
    10. 10. the gotcha’s<br />Write once; -but:<br />Test, Test, Test…<br />In other words: Triple you testing efforts<br />Different screens – Different experiences<br />Silverlight is a subset of WPF<br />But Silverlight moves faster than WPF!<br />Windows Phone is always a version behind Silverlight<br />
    11. 11. Let’s try it out <br />I can haz code?<br />
    12. 12. re-cap 1/4<br />Use compiler conditionals for platform specific code:<br />#if WINDOWS_PHONE<br /> //Windows phone 7#elif SILVERLIGHT<br /> //Silverlight<br />#else<br /> //.NET<br />#endif<br />
    13. 13. re-cap 2/4<br />Theme style declared differently:<br />Silverlight / Phone:<br />public HoverControl() {<br />#if SILVERLIGHT<br />this.DefaultStyleKey = typeof(HoverControl);<br />#endif}<br />WPF:<br />static HoverControl() {<br />#if !SILVERLIGHT<br />DefaultStyleKeyProperty.OverrideMetadata(<br />typeof(HoverControl),<br /> new FrameworkPropertyMetadata(<br />typeof(HoverControl))); <br />#endif<br />} <br />
    14. 14. re-cap 3/4<br />No VisualStateManager in WPF v3.5<br />Use WpfToolkithttp://wpf.codeplex.com<br />WPF doesn’t like linked XAML files if they are not located in the project root.<br />
    15. 15. xaml conditionals<br /> <Grid x:Name="LayoutRoot" Background="White"> <controls:HoverControl>#if SILVERLIGHT <TextBlock Text="Hello from Silverlight" />#else<TextBlock Text="Hello from .NET" />#endif</controls:HoverControl> </Grid><br />No compiler conditionals in XAML!<br />You might have to duplicate XAML <br />
    16. 16. xaml reader<br />#if SILVERLIGHT<br />element = XamlReader.Load(xaml);<br />#else<br /> using (MemoryStream xamlStream = <br /> new MemoryStream(UTF8Encoding.Default.GetBytes(<br />xaml)))<br />element = XamlReader.Load(xamlStream);<br />#endif<br />
    17. 17. create bitmap<br />BitmapImagebmi = new BitmapImage();<br />#if !SILVERLIGHTbmi.BeginInit();<br />#endif<br /> Image img = new Image();<br />bmi.UriSource= new Uri(strUrl, UriKind.Absolute);<br />#if SILVERLIGHTimg.ImageFailed+= img_ImageFailed;<br />#else<br />bmi.DownloadFailed+= bmi_DownloadFailed;<br />bmi.EndInit();<br />#endif<br />img.Source= bmi;<br /> <br />
    18. 18. stop-modify-start animations<br />#if SILVERLIGHT <br />myStoryboard.Begin(); <br />myStoryboard.Stop(); <br />#else <br />myStoryboard.Begin(element, true);<br />myStoryboard.Stop(element);<br />#endif<br />
    19. 19. in what order does what event fire?<br /><UserControl Loaded="UserControl_Loaded"><br /><my:Control Loaded="MyControl_Loaded" /><br /></UserControl> <br />Constructor<br />Loaded<br />OnApplyTemplate<br />
    20. 20. binary compatibility<br />Assembly-sharing<br />Compile for Silverlight 4<br />Reuse in WPF<br />However:<br />ONLY following assemblies supported:<br />mscorlib.dll <br />System.dll.<br />System.Core.dll<br />System.ComponentModel.Composition.dll<br />Microsoft.VisualBasic.dll<br />No UI logic!<br />Windows Phone:<br />Compile for Silverlight 3, and re-use as is*<br />
    21. 21. the bottom line:<br />Yes you CAN reuse a LOT of code, and you should!<br />…but: Develop UX for the screen<br /> Web: Links, navigation, sandboxed<br /> Desktop: Menus, multiple windows, file access<br />Phone: Touch centric, small screen<br />
    22. 22. questions?<br />
    23. 23. thank you !<br />contact info:<br />name: Morten Nielsen<br />email: mnielsen@esri.com<br />twitter: @sharpgis<br />blog: www.sharpgis.net<br />Passionate about XAML? We’re hiring!<br />www.esri.com/careers<br />

    ×