Thursday, July 30, 2009

Silverlight 3 Puzzle: Binding & DataGrid

Yesterday I had a strange bug in our application after migration to Silverlight  3.  Inner DataGrid exception was thrown when DataGrid cell with ComboBox was changed.  It took me about one day to puzzle out the bug.

I have received the following error message:

Operation is not valid due to the current state of the object.   at System.Windows.Data.BindingExpression.UpdateSource()
   at System.Windows.Controls.DataGrid.EndCellEdit(DataGridEditAction editAction, Boolean exitEditingMode, Boolean keepFocus, Boolean raiseEvents)
   at System.Windows.Controls.DataGrid.SetCurrentCellCore(Int32 columnIndex, Int32 slot, Boolean commitEdit, Boolean endRowEdit)
   at System.Windows.Controls.DataGrid.ProcessSelectionAndCurrency(Int32 columnIndex, Object item, Int32 backupSlot, DataGridSelectionAction action, Boolean scrollIntoView)
   at System.Windows.Controls.DataGrid.UpdateSelectionAndCurrency(Int32 columnIndex, Int32 slot, DataGridSelectionAction action, Boolean scrollIntoView)
   at System.Windows.Controls.DataGrid.UpdateStateOnMouseLeftButtonDown(MouseButtonEventArgs mouseButtonEventArgs, Int32 columnIndex, Int32 slot, Boolean allowEdit)
   at System.Windows.Controls.DataGridCell.DataGridCell_MouseLeftButtonDown(Object sender, MouseButtonEventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

I have debugged my application with the sources (controls: DataGrid, DataGridColumn, DataGridTemplateColumn, DataGridCell, etc.)

This error was caused by setting binding to SelectedItem property of  ComboBox twice:

  1. In cell editing template of DataGridTemplateColumn
  2. In the body of a method overriding OnPreparingCellForEdit method of DataGrid

To resolve this problem you should remove on of binding definition.

I have reproduced this bug by creating my sample below.

bug-sh

Wednesday, July 29, 2009

New features in Silverlight 3: better styles, high contrast & system colors

Today I present new features of styles and integration with operating system display settings.

Styles

Now styles can be applied more than once. It is possible to build a new style based on an existing style. You can also manage easier resources (see Merged Resource Dictionaries).

Here is my sample using these new features.

styles1-sh styles2-sh styles3-sh

High-Contrast

Users can increase the contrast in the operating system to improve interface visibility. Control code can examine the HighContrast property, and should use techniques to increase visibility when operating in high-contrast mode. On computers that run Windows, the HighContrast property is true when the user has enabled High Contrast in Control Panel. On Macintosh computers, HighContrast is true when the user has selected the White on Blackoption in Universal Access.

System colors

Silverlight 3 now supports operating system colors. In System.Windows namespace exists new static class SystemColors with colors definitions. Those colors could be use as a Color in any brushes.

Here is my sample application using the HighContrast property and SystemColors class.

contrast1-sh contrast2-sh

Monday, July 27, 2009

New features in Silverlight 3: Networking and Communication

In new version of Silverlight there are some changes in communication. I’ll write about them.

Browser or Client HTTP Handling

You can specify whether the browser or the client provides HTTP handling for your Silverlight-based applications. By default, HTTP handling is performed by the browser. In addition, you can specify the scope for the handling: all messages, a scheme (HTTP or HTTPS), a particular domain, or a single request object.

Typical scenarios with client HTTP handling:

  • Using HTTP methods other than GET and POST.

  • Using response status codes headers, response bodies in your application or for debugging purposes.

  • Sending XML HTTP requests such as REST and SOAP messages.

  • Manually managing cookies.

SOAP Faults

There are two ways to enable SOAP fault consumption:

  • You can modify your service to return SOAP faults with an HTTP status code of 200. This will make the service non-compliant with the SOAP protocol, since SOAP requires a response code in the 400 or 500 range for faults. If the service is a WCF service, you can create an endpoint behavior that plugs in a message inspector that changes the status code to 200. Then, you can create an endpoint specifically for Silverlight consumption, and apply the behavior there. Your other endpoints will still remain SOAP-compliant.
  • You can register client HTTP stack using the RegisterPrefix method. Client HTTP Handling allows you to process SOAP-compliant fault messages. However, a potential problem of switching to the alternative HTTP stack is that information stored by the browser (such as authentication cookies) will no longer be available to Silverlight, and thus certain scenarios involving secure services might stop working, or require additional code to work.

Message Credentials Authentication

A small subset of the WS-Security protocol is supported, namely, the ability to send username and password tokens in messages. When message credentials authentication is used in Silverlight, every request message will contain a SOAP header that conforms to the WS-Security protocol. The header will contain an unencrypted token with a user name and a password. You must provide them in your Silverlight code. Because the user name and password credentials are sent in plaintext (that is, unencrypted) form, you must use a secure transport (such as HTTPS) to prevent attackers from stealing the credentials while they are in transit. If you attempt to use this feature with a non-secure transport (such as HTTP), an exception will be thrown.

Other changes

  • Binary XML (binary encoding,  default encoding in Silverlight-enabled WCF Service VS template)
  • SLsvcutil.exe
  • Proxy in PollingDuplexHttpBinding

More information you can find here

I have build two samples for SOAP Faults and username authentication. I have made two versions each of them:  with  endpoint behavior and with client HTTP handling. BTW, I have little problem with reading error messages when I throw authentication or fault exceptions in username validator. Have you any idea how it should be done ?

wcf-sh

Wednesday, July 22, 2009

New features in Silverlight 3: Binding & Validation

Silverlight 3 has enhanced support for binding and data entry validation. I’ll describe it.

Binding

In Silverlight 3 you can use the ElementName property or the RelativeSource property to specify the binding source.

The ElementName property is useful when you are binding to other elements in your application.

The RelativeSource property is useful when the binding is specified in a ControlTemplate or a Style. RelativeSource markup extension has two modes:  Self or TemplatedParent. The Self mode is useful for cases where the same element should be used as the source object and target object for a binding, but different properties are the source and the target. The TemplatedParent mode is used for a data validation scenario where a template can establish the UI for error handling that occurs at run time.

In Silverlight 3 you can disable automatic source updates and update the source at times of your choosing. To do it set the UpdateSourceTrigger property to Explicit.

Validation

Silverlight 3 supports basic data validation in TwoWay bindings for target-to-source updates. Silverlight provides visual feedback for validation errors when you set the ValidatesOnExceptions property to true on the binding object. In order to receive notification that a validation error has occurred, you must also set the NotifyOnValidationError property to true on the binding object.

When you use data classes in your application, you can apply attributes to the class or members that specify validation rules, specify how the data is displayed, and set relationships between entity classes. The System.ComponentModel.DataAnnotations namespace contains the classes that are used as data attributes.

The validation and display attributes are automatically applied when used with the DataGrid control.

The ValidationSummary control displays a consolidated list of validation errors for a given container.

For more information about binding and validation look here:

I have tested all features described in the post. There are three samples in the zip file.

validation1-sh

validation2-sh 

Tuesday, July 21, 2009

New features in Silverlight 3: Pixel Shader Effects & Easing Functions

Today I’ll write about pixel shaders and easing functions.

Pixel Shader Effects

There are two pixel shader effects included with the Silverlight runtime: DropShadowEffect and BlurEffect. You can create your own effects, too.

Pixel shader effects in Silverlight are rendered in software. Any object that applies an effect will also be rendered in software. Therefore, you should be careful.

pi-invert

Easing Functions

Easing functions allow you to apply custom mathematical formulas to your animations. For example, you may want an object to realistically bounce or behave as though it were on a spring. There are many predefined functions or you may create your custom.

BackEase

BackEase EasingMode graphs.

BounceEase

BounceEase EasingMode graphs.

CircleEase

CircleEase EasingMode graphs.

CubicEase

CubicEase EasingMode graphs.

ElasticEase

ElasticEase with graphs of different easingmodes.

ExponentialEase

ExponentialEase graphs of different easingmodes.

PowerEase

QuarticEase with graphs of different easingmodes.

QuadraticEase

QuadraticEase with graphs of different easingmodes

QuarticEase

QuarticEase with graphs of different easingmodes.

QuinticEase

QuinticEase with graphs of different easingmodes.

SineEase

Cc189019.SineEase_Graph(en-us,VS.95).png

You can simply apply these easing functions to From/To/By and Key-Frame animations.

I had created simple pixel shader which inverts colors. The zip file also contains examples of other pixel shaders and easing function which I had found and I have updated to RTW.

Saturday, July 18, 2009

New features in Silverlight 3: DataGrid control & 3D Effects

What do you think about DataGrid with perspective view? It’s interesting, isn’t it?  Today I’ll give some information about new features of DataGrid control and 3D effects.

3D Effects

You can apply 3-D effects to any Silverlight UIElement using what are called "perspective transforms." Perspective transforms are not equivalent to a 3-D engine; however, they can be used to make 2-D Silverlight content appear as if it is drawn on a 3-D plane. To apply a perspective transform to a UIElement, set the UIElement object's Projection property to a PlaneProjection. The PlaneProjection defines how the transform is rendered in space. You can use the Matrix3DProjection and Matrix3D types for more complex semi-3D scenarios.

DataGrid

In Silverlight 3 you can group, sort, and filter data in a DataGrid. You should bind it to a collection view that supports these functions (PagedCollectionView class which also provides paging functionality).

I have made my sample with DataGrid control to demonstrate its new features. I have added an animation of Matrix3DProjection object, too. The zip file also contains some good samples with PlaneProjection  I have found.

datagrid3d-sh

Friday, July 17, 2009

New features in Silverlight 3: Mouse Wheel & Multitouch

Silverlight 3 introduces support for mouse wheel input and multitouch input. Today I’ll write about this features.

Mouse Wheel

MouseWheel event is only raised and able to be handled for clients running Internet Explorer and the Microsoft Windows operating system, or that are running out-of-browser. To process mouse wheel information for other clients, you must use the HTML document object model event handling for OnMouseWheel that are available to client scripting and the HTML bridge. I also have noticed that the managed event doesn’t work well with transparent visual root.

scroll

Multitouch

Multitouch requires hardware that is capable of recording touch pressure that occurs on a surface (a tablet PC device or a graphics tablet). It also requires an environment (platform and operating system, hosting application such as a browser) that can propagate the touch input to an application. You can test new Silverlight feature with Windows 7 and Internet Explorer 8. Silverlight is registered for raw touch input, not gestures. In general, it promotes raw touch input to mouse events (however, you can disable promotion).

multi-touch-SL

Thursday, July 16, 2009

New features in Silverlight 3: Out-of-Browser support

In this post I want to show out-of-browser application in the newest Silverlight.

You can configure Silverlight applications so that users can install them from their host Web pages and run them outside the browser. An out-of-browser application can run without a network connection.

out-of-browser-settings-sh

in-browser-sh

out-of-browser-on-line-sh

out-of-browser-off-line-sh

There are other several alternative hosting options for Silverlight applications, for example in a Browser Control or  as an ActiveX control in Windows-Based application. Look for more information and samples:

New features in Silverlight 3: Merged Resource Dictionaries, Navigation & Local Messaging

Today I want to write about merged dictionaries, navigation and local messaging in Silverlight 3 RTM.

Merged Resource Dictionaries

A merged resource dictionary enables you to declare the contents of a resource dictionary by referencing an external file.

Navigation

You can make cool navigation with integration with browser, URI mapping and deep linking  in a few minutes! Default XAML  in Silverlight Navigation Application and usage of UriSourceMapper have been changed since beta.

nav1-sh

mav2-sh

Local Messaging

Local messaging enables you to create communication channels between multiple Silverlight plug-ins running on a single machine. This enables you to create complex layouts that combine multiple Silverlight-based applications with content based on other technologies. The zip file contains my sample and some cool samples  I have found.

local_msg_sh

Tuesday, July 14, 2009

New features in Silverlight 3: Application Library Caching & Application Extension Services

Silverlight 3 RTM and new versions of tools has officially released last Friday. So I have started testing new features by building my own practical examples. I want to publish them regularly. I had written many practical samples and cool application for Silverlight 3 Beta, but I hadn’t published them yet. I’ll try update them to RTW and also publish in the future.

Today I’ll write about two new  features of new Silverlight: Application Library Caching and Application Extension Services.

Application Library Caching

Application library caching can help improve startup performance when users revisit your Web site. When you use application library caching, Silverlight packages some assemblies as external parts outside the .xap file. When a user first visits your Web page, the Silverlight plug-in downloads the application package and all required external parts. These files are added to the browser cache so that they can be reused on subsequent visits.

Application Extension Services

Application extension services enable you to extend the Silverlight application model. Instead of creating a custom Application subclass, you can add application extension services through the Application.ApplicationLifetimeObjects property.

Thursday, July 2, 2009

MService

Microsoft is developing DSLs for three .NET runtimes: MWeb (ASP.NET), MService (WCF) and MEntity (EF). Early MService build was presented at MIX conference on March.  The demo was very interesting, so I’ll write some details about it.

Building and testing MService at MIX was very easy:

“M” language (in one file): type, extent and service operations (Get, Post, Put, Delete, Options)

image

Deploying the file to inetpub/wwwroot catalog (IIS with MHttpHandler).

image

Testing: generated form on the service web page or client with MUrl (f.e Intellipad)

image

With the MService version we can easily build simple services. What about services with custom operations and logic?  I’m waiting for public CTP.

Wednesday, July 1, 2009

MUrl

The MUrl is a simple DSL to communicate with RESTful services. It was presented at MIX conference on March. Last time I have tested Microsoft sample with this language updated to May CTP. I’ll write some about it.

MUrl is very easy. Document included in the sample describes it well. If you want to know how to implement custom mode in Intellipad, look into the sources.

In order to execute MUrl statement, you should select it and press Ctrl+Enter.

MUrl1

Authentication is available optionally. You can define your credentials in Windows Credential Manager.

MUrl2

Now you can manage website content, for example your posts on twitter. I added and deleted some posts using Intellipad. When I tried posting I got an “Expectation Failed” error message. Solution of the problem you can find here.

MUrl3  

Download Oslo May CTP and MUrl example and have fun!