RSS
 

Archive for the ‘.Net’ Category

Creating tiled backgrounds in Metro style XAML apps

20 Jul

TL;DR version: This class will let you tile an image in a Metro-style XAML app. Use an image that’s at least 128×128 for best memory efficiency

Textures can make your application beautiful. Here’s an example from http://subtlepatterns.com, which is full of classy textures:

A nice tile texture

These textures can make great backgrounds, but because the texture is usually smaller than your application (especially tiny textures like  ), you need to repeat (tile) the image vertically and horizontally. Images that “match up” on opposite sides are ideal. All the images on  http://subtlepatterns.comhttp://bgrepeat.com and http://www.repeatxy.com are tileable.

So how do you program your application to repeat this image across a surface? In HTML, you can use the css style “background-repeat: repeat” to tile an image across your webpage. In WPF, you can set up the TileBrush properties on an ImageBrush to tile an image across your application. But in Silverlight, Silverlight for Windows Phone, or Metro style XAML applications, you’re out of luck.

Fake it

The easiest solution is to use Photoshop (or cheaper equivalent) to create a huge image that you can set as a background. Just use the pattern tool to tile your texture onto an image that’s bigger than your application’s surface will ever be. The question is: can you guarantee a maximum size? With Metro style XAML applications,  there is no maximum resolution. The other problem is that these massive image files tend to use up a LOT of memory. In my case, it was so much memory that I was likely to fail Microsoft’s Win8 certification.

Make it

There was no way I was going to convert my application into a JavaScript & HTML app, so I set out to create a control that would look just like a WPF tiled brush. The easiest way to do this is to manually arrange a set of images in a control. I’m very familiar with WPF, and I just knew there’d be some weird inconsistencies in the new WinRT APIs that made this harder than it should be, so I decided to spike this in WPF first. I inherited from Canvas, gave it an ImageSource property, and slapped my code into the OnRenderSizeChanged method. It worked perfectly, here’s the code:

    public class TileCanvas : Canvas
    {
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(TileCanvas), new UIPropertyMetadata(null, OnImageSourceChanged));

        private static void OnImageSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            ((TileCanvas)o).Rebuild();
        }

        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);
            Rebuild();
        }

        private void Rebuild()
        {
            if(ImageSource == null)
            {
                return;
            }

            var size = RenderSize;
            Children.Clear();
            var width = (int)ImageSource.Width;
            var height = (int)ImageSource.Height;
            for (int x = 0; x < size.Width; x += width)
            {
                for (int y = 0; y < size.Height; y += height)
                {
                    var image = new Image { Source = ImageSource };
                    Canvas.SetLeft(image, x);
                    Canvas.SetTop(image, y);
                    Children.Add(image);
                }
            }
            Clip = new RectangleGeometry(new Rect(size));
        }
    }

I broke a convention :(

If I was a WPF Purist I would have never extended a Panel class in order to provide a visual control. I’d have built a templated control (Inheriting from Control), and declared the canvas as a template part, and provided a default template that just contained that canvas in my Generic.xaml file. But my whole aim was to keep memory usage low, and in WPF every visual costs you memory, so I decided to pre-optimise. It’s also simpler this was as I only have to show you one file :) .

Optimising Memory

A tilebrush in WPF on a 1000×800 surface will happily repeat a 4 pixel image for you, thousands of times (200,000 to be exact), without breaking a sweat. However, with my TileCanvas, we’re creating an Image visual every time we want to repeat the tile, so you don’t want an ImageSource that’s too small. You have to find a happy balance between a huge image (which uses up a lot of memory by itself) and a lot of small images (where you don’t keep paying for the image data, but you do pay for the visual tree explosion). I have found that by repeating your pattern, Photoshop style, onto an image that’s a sized with a nice round number like 128×128 pixels is a good compromise. This applies to the WPF spike as well as the following Metro solution.

Make it – Metro Style

If you want to build a Metro Style application, you need to program against the WinRT API. This provides .NET developers with new challenges. Microsoft say that WinRT allows .NET developers to use their existing skills, which is fair. What they don’t mention is that you’re going to need a lot of new knowedge. WinRT is like a parallel universe – a lot of the things you expect to find are there, but they are a bit different, or they’re in a different place. A lot of operations are now done asyncronously, which should be make apps snappier. Truly a “parallel” universe (sorry).

When I tried to port my WPF TileCanvas over to use the WinRT version of XAML controls, I ran into two differences that changed the way I had to do things:

  • There’s no longer a RenderSizeChanged method – after a bit of experimenting, I decided on the LayoutUpdated event (although adding images causes this event to be raised, so I had to track the old size in order to avoid stack overflow)
  • ImageSources now start with a width and height of 0. It’s not until the ImageOpened event is raised that you can find out the width and height of an image. And you don’t get an ImageOpened event until your image is loaded into the Visual Tree. To get around this chicken-and-egg situation, I add a single image to the canvas, and then once it’s opened and I have the width & height I do a proper layout of the whole screen. I also attach to the ImageFailed event to help you debug, and clean up my event subscriptions once they’ve fired.

Here is my working Metro-style TileCanvas:

    public class TileCanvas : Canvas
    {
        public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(TileCanvas), new PropertyMetadata(null, ImageSourceChanged));

        private Size lastActualSize;

        public TileCanvas()
        {
            LayoutUpdated += OnLayoutUpdated;
        }

        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        private void OnLayoutUpdated(object sender, object o)
        {
            var newSize = new Size(ActualWidth, ActualHeight);
            if (lastActualSize != newSize)
            {
                lastActualSize = newSize;
                Rebuild();
            }
        }

        private static void ImageSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            TileCanvas self = (TileCanvas)o;
            var src = self.ImageSource;
            if (src != null)
            {
                var image = new Image { Source = src };
                image.ImageOpened += self.ImageOnImageOpened;
                image.ImageFailed += self.ImageOnImageFailed;

                //add it to the visual tree to kick off ImageOpened
                self.Children.Add(image);
            }
        }

        private void ImageOnImageFailed(object sender, ExceptionRoutedEventArgs exceptionRoutedEventArgs)
        {
            var image = (Image)sender;
            image.ImageOpened -= ImageOnImageOpened;
            image.ImageFailed -= ImageOnImageFailed;
            Children.Add(new TextBlock { Text = exceptionRoutedEventArgs.ErrorMessage, Foreground = new SolidColorBrush(Colors.Red) });
        }

        private void ImageOnImageOpened(object sender, RoutedEventArgs routedEventArgs)
        {
            var image = (Image) sender;
            image.ImageOpened -= ImageOnImageOpened;
            image.ImageFailed -= ImageOnImageFailed;
            Rebuild();
        }

        private void Rebuild()
        {
            var bmp = ImageSource as BitmapSource;
            if (bmp == null)
            {
                return;
            }

            var width = bmp.PixelWidth;
            var height = bmp.PixelHeight;

            if(width == 0 || height == 0)
            {
                return;
            }

            Children.Clear();
            for (int x = 0; x < ActualWidth; x += width)
            {
                for (int y = 0; y < ActualHeight; y += height)
                {
                    var image = new Image { Source = ImageSource };
                    Canvas.SetLeft(image, x);
                    Canvas.SetTop(image, y);
                    Children.Add(image);
                }
            }
            Clip = new RectangleGeometry { Rect = new Rect(0, 0, ActualWidth, ActualHeight) };
        }
    }

Almost perfect?

One thing that I would add in future would be to not throw away the entire canvas of images every time it was built. It would be better to determine just how many images need to be added and removed. At the moment, this isn’t a big issue because metro apps are always fullscreen so they don’t change size unless you snap/rotate them.

Shameless plug

I needed this class because I wanted a background texture in my new Windows 8 game, Chromazone. It’s just been accepted my Microsoft to the store, check it out: http://apps.microsoft.com/webpdp/en-US/app/chromazone/fe3aea27-d44f-43e0-8662-1c45178e4dec

 
 

Raising the right PropertyChanged with C# 5′s Caller Info Attributes

21 Sep

Implementing INotifyPropertyChanged typically requires the use of magic strings, and I’ve never been happy with the various workarounds available. They either affect performance or require a lot of extra developer ceremony (which is no good in big teams).

I’d always been hoping that C# would introduce something similar to the “typeof” operator that would help you get the name of the current property (or method) without having to use any slow reflection. Before the “//BUILD/” conference, I had given up on C# 5 bringing in anything useful here.

But then I was reading Colin Jack’s summary of the //BUILD/ talk on Future directions for C# and Visual Basic, and was particularly interested by the new Caller Info Attributes feature of C# 5. This feature is not in the CTP of VS11 yet, so I can’t test it, but I expect you’ll be able to write the following RaisePropertyChanged method:

protected void RaisePropertyChanged([CallerMemberName] string member = "")
{
    var copy = PropertyChanged;
    if(copy != null)
    {
        copy(new PropertyChangedEventArgs(this, member));
    }
}

and now when you call this method from a property setter, leaving out the optional parameter:

public string UserName
{
    get
    {
        return _userName;
    }
    set
    {
        _userName=value;
        RaisePropertyChanged();
    }
}

the compiler will fill out the parameter for you, passing “UserName” to the RaisePropertyChanged method.

What I like about this is that the compiler is creating the magic string for you on every compile – so when you rename your property, the string is automatically kept in sync. This is the best of both worlds, we get the performance benefits of a hardcoded string literal, while keeping everything DRY (and therefore hard to inadvertently break).

 

 
22 Comments

Posted in .Net, WPF

 

There’s a maximum of 255 local messaging channels in Silverlight 4

31 Jan

Don’t ask me how I got into this situation, but I found this out the hard way.

A quick search will tell you that with Silverlight Local Messaging each message has to be under forty thousand characters, but I haven’t found anything on the internet yet that tells you that you can have a maximum of 255 channels open at a time. So I’m taking responsibility and putting it out there.

Going over the limit will give you a cryptic COM exception, and channels can be “leaked” if one browser process crashes while another is open (for example iexplore.exe and chrome.exe, or just two instances of chrome.exe). Often closing all browsers will clean up the phantom channels, but not always.

Remember that other Silverlight applications might be using some of these channels too, so you should always design your application such that it uses a minimum number of channels.

 
 

Introducing Power Assert .NET

12 Nov

I’ve just released a new open source project on codeplex: http://powerassert.codeplex.com

Power Assert .NET is a .NET port of Groovy’s PowerAssert, which replaces your normal unit test assertions.

Unlike the standard assertions built into nunit, MSTest, and xunit etc; Power Assert gives you a breakdown of all the values within your assertion expression, making it quicker for you to hunt down the cause of the test failure:

System.Exception : IsTrue failed, expression was:
x + 5 == d.Month * y
| | | | | | |
| | | | | | 6
| | | | | 18
| | | | 3
| | | 01/03/2010 00:00:00
| | False
| 16
11

It’s quite a simple little thing, but it’s already saved me lots of debugging time. Give it a go :)

 
1 Comment

Posted in .Net, Testing

 

Stream an INotifyPropertyChanged property’s values through an IObservable

12 Nov

One of the new .NET technologies that is really exciting to me these days is the Reactive Extensions to .NET (RX). Sometimes it’s called LINQ to Events, because you can write LINQ queries against future values (events, nicely wrapped up into the IObservable<T> interface), instead of against existing collections (IEnumerable<T>). This technology is especially useful in stateful programming models, such as when you’re building ViewModels for WPF or Silverlight. Once you start programming with RX (although it’s got a steep learning curve!), you start to see lots of places where it would really help make your code simpler. I saw one such place: whenever you need to programatically respond to an INotifyPropertyChanged notification.

INotifyPropertyChanged

INotifyPropertyChanged is an interface that most viewmodels need to implement. It’s used implicitly by the Silverlight and WPF data binding system to register a databinding’s interest in the fact that a property has changed, thus keeping all bindings to the same property up to date. When you implement it, you’re responsible for firing a PropertyChangedEvent event every time a property changes, with the name of the property as a parameter to the event.

Sometimes, you might want to programatically watch changes to a property too. Imagine a screen where users can add rows of data that represent orders. If there needs to be a “grand total” on the screen, then one viewmodel (possibly the root VM for the whole screen) will want to subscribe to the PropertyChanged event of each of the order’s Viewmodels. However, there’s a nicer programming model for listening to updates (events etc) than the INotifyPropertyChanged event…

IObservable

IObservable is the core of all RX programming (like IEnumerable is to LINQ). If you want to expose a source of data, you need to expose an IObservable. There’s already a utility method to turn an event into an IObservable. But this Observable will publish IEvents, which isn’t quite what I was after. What I wanted was to create an IObservable of the new values of the viewmodel’s property. Just like LINQ, we can use the “Select” method to acheive this.

Funcy Expressions

An Expression is a C#3 construct that allows you to pass expression trees around. This is a very popular way for you to strongly type property names, because you can reflect over an Expression> to get the name of the Property. You can also Compile() the expression to be able to execute it. My method uses both of these: It needs to know which PropertyChangedEvents it should react to (the Where() clause), and it needs to read the new value out (the Select() clause).

The tests

Here are the unit tests for this extension method. I had to create a dummy view model class to test, named “PropertyChanger”. Note how you can supply “true” to the startWithCurrent parameter to make sure your subscription will instantly start with the current value of the property, as well as receiving new values whenever a PropertyChangedEvent is fired.

Enjoy!

 

The Curious Case of the DesignData msbuild target

18 Dec

Thanks to Karl Shifflet and Unni Ravindranathan, I’ve finally gotten my head around a superior method of populating Blend (and VS 2010′s WPF designer) with sample data. This method, which I am going to call the “DesignData build target”, has the following advantages:

  • Blend will render your control as though it’s running with real data, as you design.
  • You won’t have to look up and type magic strings when you’re creating your bindings in Blend, Blend will be able to list the possible properties for you. Intellisensational!
  • Once compiled, your output dll will have NO baggage associated with your designery frivolities.
  • You don’t have to write lots of dummy / mock / stub code
  • You don’t even have to finish writing your viewmodel: however you DO have to write all your properties. This is a Good Thing, because it means your bindings will have the right magic strings in them.

How does it work?

First, you need a xaml file that builds up a sample instance of your viewmodel. Then all you have to do is set your Blend-specific DataContext to read from that file.

How do I tell Blend where to get this design data?

I’ve already blogged about the d:DataContext attribute, letting you set up a DataContext but only inside Blend. Another secret of the “d” xml namespace* is the {d:DesignData Source=someDesignDataFile.xaml} markup extension. So all you need in your UserControl or Window’s xaml is (usually on the first element):

d:DataContext=”{d:DesignData Source=someDesignDataFile.xaml}”

Karl’s blog post explains this in a little more depth.

What should be in this design data xaml file?

Just like you usually declare and populate a Window or UserControl, you can actually declare and populate your own view-models as the root element of a xaml file.

I got myself quite confused when I started working with this, because i didn’t realise that the root element of the xaml file should be a declaration of your viewmodel (I was actually placing my viewmodel within a ResourceDictionary >< ):

image

Here you can see I’ve defined a new ScrumBoardDesignDataDemo.ViewModel.Board, and started assigning some StageNames to it. I’ve used the clr namespace of my viewmodel classes (“ScrumBoardDesignDataDemo.ViewModel”) as the default xml namespace, instead of using what xaml files usually give you: the WPF control namespace. This just cuts down on the amount of typing required.

Normally, if you’re trying to instantiate a class without a public, parameterless constructor in xaml, you’ll get a compiler error. You also won’t be able to set properties that don’t have public setters. This technique allows Blend to do a bit of magic (reflectively creating a doppelganger of your class) that means you can bend the rules a little. More on that in my next post. Let’s just concentrate on getting some sample data into Blend.

By now, if you go into Blend and you try to set up a binding through the binding dialog (you get this by clicking on the peg next to a property, say ItemsSource, and choosing Binding), you’ll get  this helpful dialog:

image

See how the “Fields” tree is populated? That’s Blend’s version of intellisense, which means you no longer have to rely on knowing what the right property name is and hand-typing it.

Don’t go away! We are only half finished. Blend might be helping you choose property names, but we’re left with two more problems:

  • Blend isn’t displaying the sample data on screen.
  • This xaml file full of things that were only useful during development is being shipped to our customers inside our DLL.

We can fix this with a simple, but quirky little feature. By setting our design data file’s build action to “DesignData” we will have both of our problems fixed. Blend will realise that it’s ok to try and instantiate the object in the xaml file, and MSBuild will leave that xaml file out of our dll.

Setting DesignData build action for the first time

Normally, if you have a look at the properties of a xaml file in visual studio you’ll see that the Build Action is set to “Page”, or in the case of App.xaml it’s “ApplicationDefinition”. You’d think that it’s just a simple matter of typing the word “DesignData” into that combobox, but if you try this, you’ll get an exception message. Karl’s blog post is about a template he wrote that gets over this problem (by creating the file with all the right properties for you), but I had to figure out how the magic worked. It turns out that it’s relatively easy to work around this, you just have to edit the csproj file manually. I’ll walk you through doing this with visual studio – although you could just as easily use notepad.

Save all your files, right click your WPF project and choose “Unload”. This puts your project into a disabled state where you can’t do anything but hand edit the xml – which is exactly what we want to do.

  1. Right click the (now greyed out) project file and choose “Edit”.
  2. Locate the design data xaml file: image
  3. Edit the element to be a “DesignData” element with no children: image
  4. Save your csproj file, right click it and choose “Reload”

Now if you compile your project, you’ll find that all traces of this design data have been erased. Even better, if you go back to blend, you’ll see that your sample data is now present (you may have to rebuild or even reload the project):

image

So hopefully you’ve got another tool in your toolbox to help you (or your graphic designer) be effective in Blend. This is my favourite method so far, however there’s a couple of situations where it might be unsuitable. My next post will be all about why, but just so you know, this doesn’t work well when:

  • You want custom logic to occur in your viewmodel at design time (ie a Color property on your viewmodel changes whenever another integer property goes below zero)
  • You are using DataTemplates with no Key, just a DataType, and are relying on WPF to find the Template by your ViewModel class. (I understand that Silverlight doesn’t support this anyway, so no problem there!)

As always, I’m keen to know if I got anything wrong. There isn’t much information about the whole “d:” namespace on the internet! Also, my third and final post in this series (about the magic that makes this work and how said magic causes the above problems) will come with a demo project.

* This namespace is actually http://schemas.microsoft.com/expression/blend/2008, but Blend always imports this as “d”

 
 

Design-time Data in Expression Blend 3: Revisited

16 Dec

How do you make sure that what you (or your designer) sees in Expression Blend is exactly what the finished product will look like? A while ago I posted about the d:DataContext attribute in Blend, and how you could leverage that to populate Blend with realistic sample data. What was awesome about that?

  • You could see exactly how the finished product would look WHILE you were designing. True WYSIWYG means efficient designers!
  • Blend would provide typesafe data binding (I don’t think I called this out, but bringing up the Binding dialog box shows a chooser for correct property names. Bye bye magic strings!)
  • Having sample data in your ItemsPresenters/ListBoxes/TreeViews means that blend will let you edit the template in place, providing for an even more useful WYSIWYG experience.

If you haven’t tried it yet, but you are a XAML developer, I really suggest you try developing a screen in Blend when it’s populated with sample data. It’s just so easy. With unit tests on your viewmodel and sample data in Blend, you can finish a screen without ever having to press F5. Okay, maybe once, but you’ll be super happy because everything will work the way it was intended.

But at what cost?

Here are the problems that I’ve found with this approach in my own projects:

I often had to write alot of code, either dummy services to be passed into the viewmodel, or lots and lots of code to simply add things into the derived viewmodel’s collections. This code (often hacked together) got deployed, bloating my DLL file. This sucks extra for Silverlight where the DLL has to be downloaded every time) and, well, looking messy.

Depending on how your viewmodel is coded, you can’t always provide a simple design-time subclass that will populate everything you want. While Dependency Injection is awesome, we aren’t all the architects on our projects, and you might get handed a viewmodel that relies on hard coded dependencies that will do yucky things like call web services, read files or connect to a database. Other times, the ways in which the services are used are so watertight that it’s impossible (or at least it would take hundreds of lines of well thought out code) to make sure you had the right sample data for your designer.

Whenever the viewmodel or the services evolved, the design-time viewmodel needed to keep up. Often (on my project), the person adding features to the viewmodel wasn’t the person who cared about the design time viewmodel. Sometimes they took the effort to make sure the design-time viewmodel compiled, other times they deleted it. Which is fair enough, they didn’t write the code, they don’t see the point of it, all its doing is helping me design the XAML. So I’d end up restoring the file from source control, adding it back into the project, and maintaining it, which was often a lot of effort!

The solution

In the meantime, I’ve been reading around the blogosphere and found a better way to do design data in blend. It doesn’t rely on Dependency Injection (although DI is lovely), it’s easy to maintain and best of all it has NO effect on your compiled DLL. Stay tuned, having gone over (in tedious detail) the why of this new approach, I’m going write a new post about how.

 
 

Timeago for C#

10 Sep

Sometimes you don’t want to make your users think. There’s the odd situation where you want to represent time in natural language: “about 4 hours ago” instead of just printing out a full timestamp. If you’re building a website, then the jQuery plugin Timeago is a pretty sweet way to do it (as long as you can stand webpages that auto update text).

Sucks for me, I’m working with WPF! (Not really sucks at all). So I needed a C# implementation of the same thing. Surely someone’s done this, right? Well my Google-fu failed me, and even when I Googled on bing I came up with nothing, so I built it myself. And I’m posting it here for you (and for me, later). If you’ve found a good one, please let me know.

First, the test cases, so you can see if the format I want is the format you want:

[TestClass]
public class FriendlyTimeDescriptionTest
{
    private static string Run(TimeSpan span)
    {
        return FriendlyTimeDescription.Describe(span);
    }

    [TestMethod]
    public void TestNow()
    {
        Assert.AreEqual("now", Run(new TimeSpan(0, 0, 0, 0)));
    }

    [TestMethod]
    public void TestSeconds()
    {
        Assert.AreEqual("1 second ago", Run(new TimeSpan(0, 0, 0, 1)));
        Assert.AreEqual("2 seconds ago", Run(new TimeSpan(0, 0, 0, 2)));
        Assert.AreEqual("59 seconds ago", Run(new TimeSpan(0, 0, 0, 59)));
    }

    [TestMethod]
    public void TestMinutesAndSeconds()
    {
        Assert.AreEqual("about 1 minute ago", Run(new TimeSpan(0, 0, 1, 1)));
        Assert.AreEqual("about 3 minutes ago", Run(new TimeSpan(0, 0, 3, 1)));
    }

    [TestMethod]
    public void TestMinutesAndSecondsRounding()
    {
        Assert.AreEqual("about 4 minutes ago", Run(new TimeSpan(0, 0, 3, 31)));
    }

    [TestMethod]
    public void TestDaysHours()
    {
        Assert.AreEqual("about 3 hours ago", Run(new TimeSpan(0, 3, 3, 1)));
        Assert.AreEqual("about 2 days ago", Run(new TimeSpan(2, 0, 1, 1)));
    }
}

I’ve conveniently wrapped all this up into an IValueConverter implementation, but if you’re not using WPF you can rip out the necessary methods. Please excuse the newline-enthused formatting – this blog theme has limited column width!

[ValueConversion(typeof(DateTime), typeof(string))]
public class FriendlyTimeDescription : IValueConverter
{
    public object Convert(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        var time = System.Convert.ToDateTime(value);
        return Describe(DateTime.Now - time);
    }

    static readonly string[] NAMES = {
                                         "day",
                                         "hour",
                                         "minute",
                                         "second"
                                     };

    public static string Describe(TimeSpan t)
    {
        int[] ints = {
                         t.Days,
                         t.Hours,
                         t.Minutes,
                         t.Seconds
                     };

        double[] doubles = {
                               t.TotalDays,
                               t.TotalHours,
                               t.TotalMinutes,
                               t.TotalSeconds
                           };

        var firstNonZero = ints
            .Select((value, index) => new { value, index })
            .FirstOrDefault(x => x.value != 0);
        if (firstNonZero == null)
        {
            return "now";
        }
        int i = firstNonZero.index;
        string prefix = (i >= 3) ? "" : "about ";
        int quantity = (int)Math.Round(doubles[i]);
        return prefix + Tense(quantity, NAMES[i]) + " ago";
    }

    public static string Tense(int quantity, string noun)
    {
        return quantity == 1
            ? "1 " + noun
            : string.Format("{0} {1}s", quantity, noun);
    }

    public object ConvertBack(
        object value,
        Type targetType,
        object parameter,
        CultureInfo culture)
    {
        return Binding.DoNothing;
    }
}

Enjoy!

 
5 Comments

Posted in .Net, Dot

 

Design-time Data in Expression Blend 3

02 Aug

Update: I’ve detailed another technique for implementing design time data in blend in this newer post (using something called DesignData)

How can you develop a complex GUI without ever needing to build and run it?

WYSIWYG tools have come a long way in recent years, and if you’re building a WPF app you can’t go past Microsoft’s newly-released Expression Blend 3. I’d always preferred Blend 2 over Visual Studio 2008 for building my XAML, not least because of the Silverlight support. But Blend 3 has some features that quite frankly put me in GUI developer nirvana. Sketchflow shows an awful lot of promise, but what’s really floating my boat is the almost undocumented design time only datacontext support. What does that even mean? Let me use a simple WPF game, Connect 4, to show you…

Lets say I’ve already built the game. Just pretend I’d even written most of the XAML by hand. Here’s what the game looks like when it’s just been launched:

running_startgame

and here’s a game that’s partway through:

running_midgame

Perfect! The game is finished. Let me tell you about how this was built:

First, I coded my ViewModel, a bunch of classes that the XAML binds to. I’ve implemented INotifyPropertyChanged where necessary, and am using ObservableCollections instead of Lists. Note that this is quite a simple game, and I don’t really have a Model (in the M-V-VM sense) to speak of:

viewmodel

The Game is the root of everything, it tracks the Players, whos turn it is, and the Columns. Columns have a collection of Cells, and Cells track whether or not  a Token has been placed within. I’ve exposed a Command on the Column class, which picks up mouse clicks through a tall, invisible button in my XAML.

The XAML for this game is quite straightforward. The window contains an ItemsControl for the columns, the column template contains an ItemsControl for the cells, and the cell template renders a blue rectangle with a hole in it, and an optional token. Grab the code and check it out if you like - you can download all this source at the bottom of the post.

Lets say that I had made this Viewmodel the datacontext of my main Window in the Window.Loaded event:

public void Window_Loaded(...){LayoutRoot.DataContext=new Game(7,5);}

When I come to edit the XAML in Blend (let’s say I was planning to make the UI look a bit foxier, with animations and gradients and whatnot), this is what I get:

No data in blend

Which is fair enough, because blend is effectively running my program with no data. The Window_Loaded event won’t be fired by Blend, Blend only works with the XAML. The Visual Studio WPF designer has the same problem:

Visual Studio with no data

A feature of WPF that has always existed, and is completely cool is that you can shift this datacontext declaration into your XAML (Note that the use of an ObjectDataProvider is optional. If we were using silverlight we’d turn these constructor arguments into properties):

xaml_datacontext

Even before Blend 3 came along, doing this would mean that Blend (and Visual Studio) would be able to render your XAML as though a game had just started:

blend_realdata

This is nice. Now we get a good idea of how the GUI will really look when it’s running. We can even edit the XAML Templates in place (to see what I mean, download the source, right click an ItemsControl and edit its ItemsTemplate). We’d be in big trouble, however, if our ViewModel tried to call anything external on construction. For example, if we were building a twitter client, we might start downloading tweets through a web service call. This may or may not work when your code is running inside blend, but you’d be working with variable results, and as soon as you have an exception your designer would die. Jonas Follesoe has a great post that talks about how you can leverage Dependency Injection to get design time data services and runtime data services going. Go read it – that’s the post that made me realise how cool and important it is to get realistic data into your design time experience.

Now that Blend 3 is out, however, we’ve got a new tool in our belt. I don’t know if you’ve ever tried to change the DesignWidth or DesignHeight of a UserControl or Window from Blend. This post explains it pretty well, if you haven’t… Well, Blend will go and add a bunch of namespace declarations to your XAML, the most important alias of which is “d”. You’ll notice that you can put d:DesignWidth in any element you like, and nothing will happen at runtime. If you’re lucky (or you put it in the right place), blend will pick up this design-time data, and will render your control at that width, but only from within blend. The fact that your XAML can contain directives that will only be executed / honoured / considered by Blend is quite a powerful one.  There aren’t very many of these attributes available, however,  Blend 3 introduced a big one: “d:DataContext”.

I stumbled across d:Datacontext when I was playing with Blend 3 and tried adding a “live datasource” to my window. I thought it was going to behave just like the old way of setting a DataContext, but when I ran my application, there was no data! I had a look at the XAML and saw that a Datacontext was being set on my root element, just like I expected. But why wasn’t this being applied at runtime? I had a really, really close look at the XAML and spotted the “d:” before the word datacontext. Sweet. I deleted that, and the app ran as expected. My next thought was to try setting both a design time (d:) and a run time (no d:) Datacontext. It worked!

So the upshot is, now we can write code like this:

Setting a runtime AND a designtime datacontext

Or we can even set the real DataContext at runtime through the Window_Loaded (or whatever) event. Why is this exciting? Now, it’s easyto have blend show a truly representative form of our gui at design time:

blend_designdata

This, by the way, was acheived with the following design time subclass of Game:

code_designgame

Simple, no? By using an ObjectDataProvider, we can even prevent the DesignTimeGame from even being instantiated at runtime. If you’re coding in silverlight, you’d use an alternative: “d:DataContext={d:DesignInstance vm:DesignTimeGame}”, which would only work with an empty constructor – which is fine, because even if the runtime game requires constructor parameters, we can just write an empty designtimegame constructor that passes some dummy ones in.

Now that we’ve got this design time data, it’s much easier to spruce things up in the XAML (note that I’m editing a template in-place, and that I deliberately coded the design time model to have a token in the top left cell):

Editing a template in place with DesignTime data

I can’t really express how much easier this design time data can make your coding. So please, download my example project, open it in blend 3, and try (for example) adding a gradient overlay to the tokens, or even just resizing them. I have to admit, I didn’t hand code the XAML, I blended it in about ten minutes once I had the design time data available to me. And I didn’t have to run it till I’d finished!

Download the Connect4 Source here

 
 

Use T4 code generation to add INotifyPropertyChanged to ADO.Net Data Services (Astoria) Entities

26 Mar

The current version of ADO.Net Data Services (Astoria) for Silverlight generates some extremely Plain Old CLR Objects. As Shawn Wildermuth notes, you dont even get INotifyPropertyChanged support. You might need this if you want to databind directly to your Astoria entities, but if you’re doing serious Silverlight work, it pays to wrap your entities up in a ViewModel. Even if you’re doing this, having INotifyPropertyChanged support can really help – for example, instead of saving every single entity on your model, you only need to fire the ones that change. For example:

entity.PropertyChanged += (sender, e) => dataContext.UpdateObject(sender);

Pretty soon, this functionality will be built into Astoria. In the meantime, however, it’s possible to add this functionality thanks to the partial methods in the web reference-generated entity. When you add an Astoria web reference to a silverlight project, the two most interesting things that get added (expand the service reference to see these files) are:

Reference.cs

This is the C# file that contains all of the code relating to the service reference. Look inside and you will see your DataContext and all of your Entities. Note that your entities don’t fire property changed events, but they do define partial methods:

        
    public string City
    {
        get
        {
            return this._City;
        }
        set
        {
            this.OnCityChanging(value);
            this._City = value;
            this.OnCityChanged();
        }
    }
    private string _City;
    partial void OnCityChanging(string value);
    partial void OnCityChanged();

service.edmx

This is an XML file that defines, among other things, each of the entities in your service reference:

      <EntityType Name="Address">
        <Key>
          <PropertyRef Name="ID" />
        </Key>
        <Property Name="ID" Type="Edm.Int32" Nullable="false" />
        <Property Name="Line1" Type="Edm.String" Nullable="true" />
        <Property Name="Line2" Type="Edm.String" Nullable="true" />
        <Property Name="City" Type="Edm.String" Nullable="true" />
        <Property Name="Country" Type="Edm.String" Nullable="true" />
      </EntityType>

This XML file serves as the perfect source for our data generation template!

DataServiceINotifyPropertyChanged

If you’re not familiar with T4 code generation and it’s integration into visual studio, now would be a good time to go and read Oleg Sych’s blog - he covers EVERYTHING. Or you could just go ahead and incorporate my template into your Silverlight project – it works on my machine! 

In order to make the namespace of the generated file match the namespace of your service classes, you need to put this template into a top level folder that matches the name of your service reference. The template will use that directory name to find the edmx file in question:

T4 Template

 

Basically, the template loops over all the entities in the edmx, generates a partial class (which will match the service entities), then loops over all the properties and generated a partial method implementation. Easy?

Download the template file here. You might have to change the extension from txt to tt…

 
4 Comments

Posted in .Net