Sharing Code between Windows, WinRT and Windows Phone

Monday, December 16, 2013

In recent times, I often find myself developing for more than one “Windows” platform – typically Windows Phone and Windows 8 Store and sometimes Windows (WPF) as well. In this post, I’d like to share some of the tools and techniques I’ve been using to ease code sharing. Portable Class Libraries (PCLs) PCLs came out in Visual Studio 2012 and provide an easy way to create a single project that can be referenced by multiple project types. When you create a PCL, you get the following dialog: This dialog allows you to select multiple targeted platforms (at...

XAML Tip: Graphics with ItemsControl

Saturday, February 23, 2013

Sometimes in a WPF or Windows Store or Windows Phone application we need to draw some things based on some collection of data items. Suppose we have the following simple data item: class CarData { public double Distance { get; set; } public string Image { get; set; }}Suppose we have a collection of CarData objects, and the requirement was to show a set of images along a line with a particular distance, like in the following screenshot:The distance from the left is determined by the Distance property, and the image is determined by...

Preview of Blend 5 available

Tuesday, December 25, 2012

When Visual Studio 2012 came out, one thing was sorely missing. An Expression Blend tool that can handle WPF 4.5 applications. Blend for Visual Studio that is currently available only supports Windows 8 Store Apps, but not WPF (or Silverlight).A few days ago, Microsoft released a preview of Blend 5 (with Sketchflow), that’s able to work with WPF and Silverlight, along with Windows 8 Store apps.The tool can be downloaded from here: stated that the final version of the tool will be available with Update 2 of Visual Studio 2012 (Update 1 was released about 2 weeks ago).
no comments

Windows Phone: Playing Sound Effects

Monday, September 3, 2012

I wanted to create a simple timer application for Windows Phone. The timer would count backwards from a user configured value and when it reached zero, it would play some sound effect to indicate expiration. Sounds simple enough…This is how the application looks like:The problem turned out to be playing that sound effect. I had a WAV file I wanted to play. Silverlight has a MediaElement object that is capable of playing video and/or audio, so it seemed to be a good candidate for the job. MediaElement is an element, so must be placed somewhere in the visual tree....
no comments

Windows 8 Metro: Detecting scroll changes in ListView

Monday, July 2, 2012

I had a requirement in a Metro app I’m working on to detect scrolling in a ListView (GridView is practically the same), or more precisely, detect whether the selected item goes off the visible ListView area, and if so, switch some items in the ListView so that the selected item be visible again; this is not an entirely accurate description, but it’s close enough for our purposes. An easy one, right?Searching the ListView class (and its bases) yields no useful results on scrolling. In WPF, the ScrollViewer element has an attached event, ScrollChanged. This can be used (in WPF)...

INotifyPropertyChanged implementation with C# 5.0

Tuesday, March 20, 2012

The INotifyPropertyChanged interface has become a very popular interface, typically used in MVVM scenarios (WPF/Silverlight). It looks simple, with a single member, which is an event, and its basic implementation by some data type may be something like this: class Book : INotifyPropertyChanged {     public event PropertyChangedEventHandler PropertyChanged;       protected virtual void OnPropertyChanged(string propName) {         var pc = PropertyChanged;         if(pc != null)             pc(this, new PropertyChangedEventArgs(propName));     }       string _name;     int _published;       public int Published {         get { return _published; }         set {             if(_published != value) {                 _published = value;                 OnPropertyChanged("Published");             }         }     }  ...

WPF/Silverlight Tip: Transparent Hit Testing

Thursday, February 23, 2012

Hit testing is the process of finding out which elements (if any) contain a certain point (typically the location of the mouse pointer). Sometimes, however, there is a need to disregard some elements in a hit testing scenario. Consider this simple program that allows moving of circles: One can grab a circle and drag it around. Here’s the MouseLeftButtonDown event handler on the containing Canvas: void OnMouseDown(object sender, MouseButtonEventArgs e) {     var shape = e.Source as Shape;     if(shape != null) {         _canvas.CaptureMouse();         _moving = true;         _current = e.GetPosition(_canvas);         _currentShape = (FrameworkElement)shape.Parent;     } } ...
no comments

A UniformGrid for Silverlight/Windows Phone

Tuesday, February 7, 2012

The UniformGrid panel in WPF has some useful features, especially as an items panel in an ItemsControl. I blogged about the usefulness of the UniformGrid here. But what about Silverlight? It has no UniformGrid, but we can create one as a custom panel. This would be usable in Silverlight for the desktop and for Windows Phone, and would be a simple enough example to show in one post. The layout process In WPF/Silverlight, layout is a two step process. The first step is Measure: the panel asks each child it’s hosting what size it would like to be, given...

WPF Tip: Displaying Images in different Pixel formats

Monday, January 30, 2012

If we want to show an image in WPF, we typically use an Image element and connect its Source property to some image resource within our project: <Image Source="Penguins.jpg" /> The Source property is not a string, it’s an ImageSource – an abstract type with several concrete implementations that provide a “real” image source. The above markup works thanks to the help of a type converter, that makes the source a BitmapImage – one of the simplest sources, that presents the image as is. What if we wanted to show the...

First Meeting of the Windows Devices Israel User Group

Tuesday, January 17, 2012

Yesterday evening we’ve held the first meeting of the new Windows Devices user group at Microsoft offices in Ra’anana. This first meeting was dedicated to Windows Phone 7. Elad and I had a lot of fun and we hope you guys had fun as well and learned something along the way. What we’ve done is talk about the Windows Phone platform in general, its capabilities, the tools used to write applications and games, while showing a few demos. We tried to answer as many general questions as we could, and those questions are definitely a basis for future meetings....