Logo Subtitle Subtitle

Split Generic.xaml in Silverlight Applications

If you work with Templated controls in a big Silverlight project, your Generic.xaml might grow fast. Here’s a quick tutorial on how to split the Generic.xaml into multiple resource files.

Step1: Find the resource

You will typically have the control code:

public class TemplatedControl1 : Control {
    public TemplatedControl1() {
        this.DefaultStyleKey = typeof(TemplatedControl1);
    }
}

and the XAML in the Generic.xaml:

<Style TargetType="local:TemplatedControl1">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:TemplatedControl1">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Step2: Create a new resource file

Create a copy of Generic.xaml and rename to TemplatedControl1.xaml.
Delete the TemplatedControl1 style from Generic.xaml.
So Generic.xaml looks like:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication1">
</ResourceDictionary>

and TemplatedControl1.xaml looks like:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SilverlightApplication1">

    <Style TargetType="local:TemplatedControl1">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:TemplatedControl1">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Step3: Create reference in App.xaml

Open App.xaml and add the resource:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="SilverlightApplication1.App"
             >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source='/SilverlightApplication1;Component/Themes/TemplatedControl1.xaml'/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

If you open the project in Expression Blend you will see that you can edit the template as usual:

Seven Silverlight snippets you can not live without

One of the best Visual Studio features is the ability to create code snippets. During our Silverlight Training we use many of them. Here is a selection of the ones we use the most. You can download a zipfile with snippets and copy them to:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

or

c:\Users\You\Documents\Visual Studio 2010\Code Snippets\Visual C#\

1 clsObservableObject

Creates an ObservableObject class you can use to subclass your Models from. Each model that subclasses ObservableObject can call a typed or untyped OnPropertyChanged method.

Output code:

public class ObservableObject : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName) {
        var handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void OnPropertyChanged<TViewModel>(Expression<Func<TViewModel>> property) {
        var expression = property.Body as MemberExpression;
        var member = expression.Member;

        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(member.Name));
        }
    }
}

2 event

Event declaration and a helper method to raise it.

Output code:

public event EventHandler DoubleClick;

protected virtual void OnDoubleClick() {
    EventHandler handler = DoubleClick;
    if (handler != null) {
        handler(this, EventArgs.Empty);
    }
}

3 loremIpsum

Creates a string with lorem ipsum text.

Output code:

var loremIpsum="Lorem ipsum dolor sit amet, Toverstudio consectetuer...

4 propds

Creates a Silverlight dependency property

Output code:

public string Text {
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

/// <summary>
/// Identifies the Text dependency property. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text",
                                typeof(string),
                                typeof(MyControl),
                                new PropertyMetadata(null, OnTextPropertyChanged));

/// <summary>
/// Text changed handler.
/// </summary>
/// <param name="d">MyControl that changed its Text.</param>
/// <param name="e">DependencyPropertyChangedEventArgs.</param>
private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    var source = d as MyControl;
    if (source != null) {
        var value = (string)e.NewValue;
        //TODO: Handle new value.
    }
}

5 propds_nocallback

Creates a Silverlight dependency property without a callback method

Output code:

public string Caption {
    get { return (string)GetValue(CaptionProperty); }
    set { SetValue(CaptionProperty, value); }
}

public static readonly DependencyProperty CaptionProperty =
    DependencyProperty.Register("Caption",
                                typeof(string),
                                typeof(MyControl),
                                new PropertyMetadata(null));

6 propNotify

Uses the OnPropertyChanged with a string parameter (“”) from ObservableObject.

Output code:

private string name;

public string Name {
    get { return name; }
    set {
        if (name != value) {
            name = value;
            OnPropertyChanged("Name");
        }
    }
}

7 propNotify_typed

Uses the typed OnPropertyChanged method from ObservableObject.

Output code:

private string name;
public string Name {
    get { return name; }
    set {
        if (name != value) {
            name = value;
            OnPropertyChanged(() => this.Name);
        }
    }
}

You can download a zipfile with the snippets

Open dual side-by-side finder with applescript

…and save as an app on your desktop!

create an app with applescript to open two finders side-by-side in a few minutes!

I use both a Mac and Windows machine and if I may compare the two, I would say one of them is my feel-good-machine and the other a workhorse to develop .NET stuff on. Both OSses have file managers which work great for finding and opening files but when you copy, move and view files all the time, doing that in the windows explorer or the Mac Finder soon becomes a click fest.

TotalCommander

On my windows machines I use the brilliant Total Commander which was very accurately described by someone as: “Using windows without Total Commander is like driving without my glasses”. It’s true. This thing can do anything from file management to comparing files and from FTP to batch copy.

Back to the Finder

When I bought my Mac I searched for Total Commander alternatives and while some of them work OK, they are not perfect. So while I wait and hope one day Christian Ghistler will re-write Total Commander for the Mac, I’m just going to use the Finder. But to copy and move files, I really need to have side by side Finders. Of course you can open two Finders but why not have an AppleScript do that for me? I googled, found this one to start from and made some changes like determining the screen size automatically and place the finders side-by-side around the center of the screen.

AppleScript to open two Finders side-by-side

STEP1: Fire up the AppleScript Editor and paste this code in it:

-- Feel free to copy, modify and redistribute this script any way you like
-- Be careful to run this "AS IS" script on your machine. I've
-- tested it and it works on my iMac but if you don't know what you're doing,
-- you could end up hurting your beloved Mac and the data on it.
-- Cheers, Loek

-- This script opens two finder windows side-by-side
property finderWidth : 900 -- MUST BE SMALLER THEN HALF OF THE ACTUAL SCREEN SIZE!
property finderHeight : 700

tell application "Finder"
	set screenBounds to bounds of window of desktop
	set screenWidth to item 3 of screenBounds
	set screenHeight to item 4 of screenBounds
	set centerX to screenWidth / 2
	set centerY to screenHeight / 2
	activate
	set visible of (every process whose visible is true and frontmost is false) to false
	set finder1 to make new Finder window
	set the bounds of finder1 to {centerX - finderWidth div 1, centerY - (finderHeight / 2) div 1, centerX - 2 div 1, centerY + (finderHeight / 2) div 1}
	set the current view of finder1 to column view
	set finder2 to make new Finder window
	set the bounds of finder2 to {centerX + 2, centerY - (finderHeight / 2) div 1, centerX + finderWidth, centerY + (finderHeight / 2) div 1}
	set the current view of finder2 to column view
end tell

STEP2: Set a correct value for the finderWidth and finderHeight and if you run the script, two Finders should open. If not, make sure the finderWidth is less then half of the actual screen width.

STEP3: Save as app

In the AppleScriptEditer goto File | Save As…

Type a name and choose Application as File Format

Open the DualFinder.app et voila! Two Finders side by side!

Open radio streams in iTunes

The Radio function in iTunes lets you browse stations but it won’t let you search for one. If you know a station that shares a music stream, you can open it directly in iTunes.

Find a stream

Google for radio streams and find playlists that end with .m3u. Copy the stream address to your clipboard.

Open in iTunes

In iTunes go to Advanced > Open stream (CMD+U) and enter the address. For your convenience, you can drag the item from the main grid to your playlists for later listening.

Eindhoven Glow 2011

This week is the 5th Glow festival in Eindhoven. Being born in the heart of Eindhoven I really enjoy seeing Eindhoven becoming more and more design and history (Philips, Light) aware and Glow is a great initiative where light and design meet. I shot this video with my Windows Phone yesterday:

More Architectural Projection

I personally like the architectural projections the most and here are some of my favorites:

http://www.youtube.com/watch?v=BGXcfvWhdDQ

http://www.youtube.com/watch?v=gE3Zg_sa0iY&feature=related