Exam Preparation–Silverlight 4, Development–70-506–Part 3

I’m currently studying for the Beta exam of Silverlight 4. So I thought, why not share what resources I’m using.

To start with one should be familiar with Silverlight of course, but refreshing some of the topics that haven’t be touched that often is always good. This is by far not the definitive learning guide for the exam, but it’s a start. If you know articles that should be in either one of the categories please let me know.

What Skills are being Measured?

You can start on the Microsoft Learning site first: Exam 70-506: TS: Silverlight 4, Development.

The are 7 main categories of topics that are measured (not sure what the last 1% is).

Laying Out a User Interface (15%)

Enhancing the User Interface (14%)

Implementing Application Logic (16%)

Working with Data (17%)

Interacting with a Host Platform (11%)

Structuring Applications (13%)

Deploying Applications (13%)

Working with Data

Format data.

Formatting string values in databinding: http://wildermuth.com/2009/11/18/Data_Binding_Changes_in_Silverlight_4
Formatting culture-specific string values: http://www.eggheadcafe.com/sample-code/csharp.NET/54cf2c87-d818-4fbf-a1f3-04972e09a633/converts-the-value-of-a-specified-object-to-an-equivalent-string-representation-using-specified-format-and-culturespecific-formatting-information.aspx

Implement data binding.

Basic Data Binding: http://odetocode.com/Articles/740.aspx
Binding to elements: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind
Implementing ObservableCollection: http://weblogs.asp.net/joelvarty/archive/2008/11/17/silverlight-databinding-the-observable-collection.aspx
Setting a fallback value: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#stringformat

Create and consume value converters.

http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx

Implement data validation.

Implementing IDataErrorInfo: http://johnpapa.net/silverlight/enabling-validation-in-silverlight-4-with-idataerrorinfo/
Implementing INotifyDataErrorInfo: http://www.silverlight.net/learn/whitepapers/introducing-new-inotifydataerrorinfo-interface/#_Toc246386924
Data Binding Errors: http://www.codeproject.com/Articles/86955/Silverlight-4-Data-Validation-Tip-of-the-Day-Part-.aspx

Exam Preparation–Silverlight 4, Development–70-506–Part 2

I’m currently studying for the Beta exam of Silverlight 4. So I thought, why not share what resources I’m using.

To start with one should be familiar with Silverlight of course, but refreshing some of the topics that haven’t be touched that often is always good. This is by far not the definitive learning guide for the exam, but it’s a start. If you know articles that should be in either one of the categories please let me know.

What Skills are being Measured?

You can start on the Microsoft Learning site first: Exam 70-506: TS: Silverlight 4, Development.

The are 7 main categories of topics that are measured (not sure what the last 1% is).

Laying Out a User Interface (15%)

Enhancing the User Interface (14%)

Implementing Application Logic (16%)

Working with Data (17%)

Interacting with a Host Platform (11%)

Structuring Applications (13%)

Deploying Applications (13%)

 

Resources for Interacting with a Host Platform

Implement the printing API.

General printing API: http://www.silverlightshow.net/items/A-look-at-the-Printing-API-in-Silverlight-4.aspx
http://channel9.msdn.com/shows/SilverlightTV/Advanced-Silverlight-Printing-Strategies-Silverlight-TV-35/

Handling multiple pages: http://www.silverlightshow.net/items/Advanced-printing-in-Silverlight-4.aspx

 

Create out-of-browser applications.

Out of browser experiences (Network connectivity, installing and uninstalling applications, checking and updating the applicatioin versions): http://www.silverlight.net/learn/videos/all/out-of-browser-experiences/

Toast Notifications: http://www.wintellect.com/CS/blogs/jprosise/archive/2010/01/09/silverlight-4-s-new-notification-windows.aspx

Application chrome: http://geekswithblogs.net/tkokke/archive/2010/06/28/custom-window-chrome.aspx

Enabling trusted applications, COM Interop: http://pendsevikram.blogspot.com/2010/01/silverlight-4-com-interopendless.html

Hosting Web browser control: http://www.silverlight.net/learn/videos/all/hosting-html-content/

 

Access isolated storage.

Reading and Writing to Isolated storage: http://www.silverlight.net/learn/quickstarts/isolatedstorage/

Changing Quota: http://blogs.silverlight.net/blogs/msnow/archive/2008/07/17/tip-of-the-day-20-how-to-increase-the-isolated-storage-quota-for-your-app.aspx


Interact with the HTML DOM.

Accessing cookies: http://blogs.silverlight.net/blogs/msnow/archive/2008/07/15/tip-of-the-day-18-how-to-set-browser-cookies.aspx

Querystring values: http://thepursuitofalife.com/getting-query-string-values-in-silverlight/

Javascript and Silverlight: http://blogs.silverlight.net/blogs/msnow/archive/2008/07/08/tip-of-the-day-15-communicating-between-javascript-amp-silverlight.aspx

Silverlight to Silverlight: http://mark.mymonster.nl/2009/10/07/silverlight-3-local-messaging-explained-enhancement/

Interacting with the DOM: http://www.silverlightshow.net/items/Interaction-between-Silverlight-and-HTM.aspx

 

Access the clipboard.

http://www.silverlight.net/learn/videos/all/accessing-global-clipboard/

 

Read from and write to the host file system.

Local File Access: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#localfiles

Drop files from the file system: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#droptarget

OpenFileDialog: http://msdn.microsoft.com/en-us/library/cc221415(VS.95).aspx

SaveFileDialog: http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx

 

Handle alternative input methods.

Right-click: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#rightclick

Mouse wheel: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#mousewheel

Multi-touch events: http://timheuer.com/blog/archive/2009/07/30/silverlight-3-multi-touch-introduction-fundamentals-basics.aspx

Exam Preparation–Silverlight 4, Development–70-506–Part 1

I’m currently studying for the Beta exam of Silverlight 4. So I thought, why not share what resources I’m using.

To start with one should be familiar with Silverlight of course, but refreshing some of the topics that haven’t be touched that often is always good. This is by far not the definitive learning guide for the exam, but it’s a start. If you know articles that should be in either one of the categories please let me know.

What Skills are being Measured?

You can start on the Microsoft Learning site first: Exam 70-506: TS: Silverlight 4, Development.

The are 7 main categories of topics that are measured (not sure what the last 1% is).

Laying Out a User Interface (15%)

Enhancing the User Interface (14%)

Implementing Application Logic (16%)

Working with Data (17%)

Interacting with a Host Platform (11%)

Structuring Applications (13%)

Deploying Applications (13%)

 

Resources for Laying Out a User Interface

Arrange content with panels.

Grid: http://www.silverlightshow.net/items/Using-Silverlight-Grid-Control.aspx

Stack Panel: http://www.silverlightshow.net/items/Using-the-StackPanel-control-in-Silverlight-2-Beta-1.aspx

Canvas: http://www.a2zdotnet.com/View.aspx?id=117

Border: http://www.silverlightshow.net/items/Using-the-Border-control-in-Silverlight-2-Beta-1-.aspx

ScrollViewer: http://www.c-sharpcorner.com/UploadFile/mamta_m/307312009072507AM/3.aspx

ViewBox: http://blogs.silverlight.net/blogs/justinangel/archive/2008/11/06/silverlight-toolkit-viewbox.aspx

 

Implement and configure core controls.

TextBox: http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightTextBox12252008202141PM/SilverlightTextBox.aspx

Button: http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightButton11182008005631AM/SilverlightButton.aspx

TextBlock: http://visualstudiomagazine.com/articles/2008/05/12/silverlight-xaml-primer-15-enhancing-textblock-with-runs-and-linebreaks.aspx

CheckBox: http://www.c-sharpcorner.com/UploadFile/mahesh/SilverlightCheckBox11282008214151PM/SilverlightCheckBox.aspx

Content Controls: http://www.silverlight.net/learn/quickstarts/choosing-which-control-to-use/#SingleElement

 

Create user controls.

http://www.silverlightshow.net/items/Creating-a-Silverlight-Custom-Control-The-Basics.aspx

http://msdn.microsoft.com/en-us/library/cc278064(VS.95).aspx

http://www.silverlightshow.net/items/Creating-a-Skinnable-Custom-Control-in-Silverlight-3-Part-I.aspx

http://www.silverlightshow.net/items/Creating-a-simple-Voting-control-in-Silverlight-2.aspx

 

Implement a navigation framework.

http://www.silverlightshow.net/items/The-Silverlight-3-Navigation-Framework.aspx

http://silverlight.net/learn/learnvideo.aspx?video=187319

 

Display collection of items.

Items control: http://wildermuth.com/2009/01/18/Fun_with_ItemsControl

ListBox: http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-5-using-the-listbox-and-databinding-to-display-list-data.aspx

TabControl: http://timheuer.com/blog/archive/2008/06/04/silverlight-2-introduces-tabcontrol.aspx

Headered Items Control: http://blogs.silverlight.net/blogs/justinangel/archive/2008/11/11/silverlight-toolkit-headeredcontentcontrol-amp-headereditemscontrol.aspx

TreeView: http://blogs.silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treeview-treeviewitem-amp-hierarchaldatatemplate.aspx

DataGrid: http://www.c-sharpcorner.com/UploadFile/mahesh/SLDataGrid05032009223331PM/SLDataGrid.aspx

 

Play media files.

Implementing digital rights management (DRM): http://msdn.microsoft.com/en-us/library/cc838192(v=VS.95).aspx

Playing streams: http://www.learn-silverlight-tutorial.com/StreamingMediaUsingSilverlight.cfm

Creating Timeline markers: http://www.silverlight.net/learn/videos/silverlight-media-framework/creating-custom-timeline-markers/

Respons to timeline marker events: http://msdn.microsoft.com/en-us/library/cc189078(VS.95).aspx#anchor_mediaelement_mediamarkers

Shake that Windows Phone 7 and detect it

Alright, when you look at what apps are popular, you’ll see a lot of apps that interact with the accelerometer in a way like: Shake your phone baby!

Yes we have the accelerometer on the Windows Phone 7 as well, how to use it? That’s simple.

1: Reference the Microsoft.Devices.Sensors assembly.

Add a Add a reference to the Microsoft.Devices.Sensors assembly

2: Create an instance of the Accelerometer.

private readonly Accelerometer _sensor = new Accelerometer();

3: Start listening to the ReadingChanged

_sensor.ReadingChanged += ReadingChanged;

private void ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
}

The structure of the AccelerometerReadingEventArgs looks like this.

public class AccelerometerReadingEventArgs : EventArgs
{
    public double X { get; }
    public double Y { get; }
    public double Z { get; }
    public DateTimeOffset Timestamp { get; }
}

Pretty simple, X, Y and Z coordinates, how to read them?

image

Source: http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/09/08/using-the-accelerometer-on-windows-phone-7.aspx

Shake it

That’s about it for the basics. You can do a lot of things with the accelerometer. I want to know when the device is shaken. I started with a bit of google, and found: Shake Detection by Mark Arteaga. Nice article with a good explanation of the code. Though, he didn’t have a chance to test the code, and it was also based on a pre-release version of the Developer Tools. So after some adjustments I came with the following code that both compiles in the final Windows Phone 7 Developers Tools and also runs on the LG Prototype.

public class AccelerometerSensorWithShakeDetection : IDisposable
{
    private const double ShakeThreshold = 0.7;
    private readonly Accelerometer _sensor = new Accelerometer();
    private AccelerometerReadingEventArgs _lastReading;
    private int _shakeCount;
    private bool _shaking;

    public AccelerometerSensorWithShakeDetection()
    {
        var sensor = new Accelerometer();
        if (sensor.State == SensorState.NotSupported)
            throw new NotSupportedException("Accelerometer not supported on this device");
        _sensor = sensor;
    }

    public SensorState State
    {
        get { return _sensor.State; }
    }

    #region IDisposable Members

    public void Dispose()
    {
        if (_sensor != null)
            _sensor.Dispose();
    }

    #endregion

    private event EventHandler ShakeDetectedHandler;

    public event EventHandler ShakeDetected
    {
        add
        {
            ShakeDetectedHandler += value;
                
        }
        remove
        {
            ShakeDetectedHandler -= value;
            _sensor.ReadingChanged -= ReadingChanged;
        }
    }

    public void Start()
    {
        if (_sensor != null)
            _sensor.Start();
    }

    public void Stop()
    {
        if (_sensor != null)
            _sensor.Stop();
    }

    private void ReadingChanged(object sender, AccelerometerReadingEventArgs e)
    {
        //Code for checking shake detection
        if (_sensor.State == SensorState.Ready)
        {
            AccelerometerReadingEventArgs reading = e;
            try
            {
                if (_lastReading != null)
                {
                    if (!_shaking && CheckForShake(_lastReading, reading, ShakeThreshold) && _shakeCount >= 1)
                    {
                        //We are shaking
                        _shaking = true;
                        _shakeCount = 0;
                        OnShakeDetected();
                    }
                    else if (CheckForShake(_lastReading, reading, ShakeThreshold))
                    {
                        _shakeCount++;
                    }
                    else if (!CheckForShake(_lastReading, reading, 0.2))
                    {
                        _shakeCount = 0;
                        _shaking = false;
                    }
                }
                _lastReading = reading;
            }
            catch
            {
                /* ignore errors */
            }
        }
    }

    private void OnShakeDetected()
    {
        if (ShakeDetectedHandler != null)
            ShakeDetectedHandler(this, EventArgs.Empty);
    }

    private static bool CheckForShake(AccelerometerReadingEventArgs last, AccelerometerReadingEventArgs current,
                                        double threshold)
    {
        double deltaX = Math.Abs((last.X - current.X));
        double deltaY = Math.Abs((last.Y - current.Y));
        double deltaZ = Math.Abs((last.Z - current.Z));

        return (deltaX > threshold && deltaY > threshold) ||
                (deltaX > threshold && deltaZ > threshold) ||
                (deltaY > threshold && deltaZ > threshold);
    }
}

And of course you want to use it in your app, which can be done like this.

_shakeSensor = new AccelerometerSensorWithShakeDetection();
Loaded += (sender, args) =>
                {
                    _shakeSensor.ShakeDetected += ShakeDetected;
                    _shakeSensor.Start();
                };
Unloaded += (sender, args) =>
                {
                    _shakeSensor.ShakeDetected -= ShakeDetected;
                    _shakeSensor.Stop();
                };
Be aware that the event is fired on a different thread, so if you want to use this in the UI thread you might want to read an old article on UI threads from my hand.

What did I do last 115 days?

Yes it’s already 115 days ago since my last blog post. So time for some explanation.

Windows Phone 7

First of all I’ve been involved in a Windows Phone 7 project, actually two projects. I’ve been asked by buienradar.nl to create a Windows Phone 7 companion app for their website, that means a rainfall radar application for Belgium and The Netherlands and another rainfall radar application for Germany niederschlagsradar.de.

The WP7 buienradar.nl application
The WP7 nierderschlagsradar.de application

Both applications had to be ready before the launch, so we did our best to get the apps ready end of September. But after that, we had to wait before we were able to submit the apps to the marketplace, which we weren’t able to until last week. The first application is now accepted and can be downloaded through the marketplace. For the nierderschlagsradar.de application we’re still waiting for the completion of testing.

Sixin – Silverlight and Expression Insiders User Group

Also as a member of the Sixin user group I organized an event on the 14th of September. Although a bad weather caused for a lot of long Traffic Jams we’ve had quite a successful meeting. Gill Cleeren, well known in the community presented on MEF and Databinding in Silverlight.

So end all, I was not gone, but a little bit busy.