Tuesday, August 11, 2009 #

Behavior to Control DataForm AutoGenerate

The DataForm like the DataGrid control by default has the ability to auto generate fields based on the public properties of the object bound it.  This is controlled by the AutoGenerateFields property which is set to true by default.  I think the most common use for this feature is building demo apps and prototyping.  In those types of uses you can usually overlook the fact that there are a few properties with fields generated that you would never show a user of a business application.  In the rest of this blog post I will walk you through how you can add more control using a Silverlight 3 Behavior.

You can now also have more control over field generation by adding attributes to the class that you are binding to using the Editable and Display attributes.  Editable allows you to mark the property to be treated as read only or generate a one way binding.  Display as used below sets the AutoGenerateField property to false to ensure that the Age property doesn’t generate a UI Element.

image

You might have done similar things during the Silverlight 3 beta using the Bindable attribute which was replaced by using Display and Editable – see the change doc for more details. 

If you are using .NET RIA Services it helps by projecting attributes you place on the server side class metadata to the Silverlight client code generated.  When binding a class projected to the client by RIA services no extra work is required because these attributes are there.

These attributes help a lot and make it so there are even more places where the DataForm can be used.  One of the problems though is there are times where either you don’t want to mess with meta data, don’t have control over the class to add meta data, want a different set of fields included or excluded for a particular use.  In any of those cases probably the most common suggested work around is setting the AutoGenerateFields to false and specify a specific list of fields.  That can get real tedious real quick especially if you just wanted to hide a couple of fields.

The DataForm provides a useful event to help out here it’s called AutoGeneratingField.  This event is called as the DataForm is about to generate a field for a property.  In the event arguments you are passed the property name, the property type, a Field property and finally a Cancel flag.  The Field property is used if you want to provide a DataField back based on your own evaluation of the property name or type.  The Cancel flag though is what were most interested in for this blog post because it allows you to cancel generation of a field.  Using an event handler on that event you could accomplish the same thing we did with the attribute above as you can see in the following example.

image

While that’s helpful, I was really hoping for a way to package it up to be a little more reusable.  One way you could do that is inherit your own MyDataForm class from DataForm and override the OnAutoGenerateField method as you can see in the following example.

image 

I’ve of course over simplified this a little but you get the point.  I basically have a property that can pass in the fields I want and if not in the list I cancel the generation.  I would probably also include an ExcludeFields property as well so you could stop one or two fields from generating.  Both can be useful in making the DataForm useful for more scenarios.  The problem with this approach is that you then have to always use the MyDataForm class to get the benefit.  In the next example we will look at how to use a Silverlight Behavior.

Behaviors and Triggers are a new feature that we gained with Silverlight 3 and are packaged as part of the blend SDK in the System.Windows.Interactivity assembly.  They are nice because they let you add capabilities to existing controls without having to inherit from the control. In the rest of this post we are going to build a behavior to allow us to filter the fields.  The first thing that needs to be done after we add a reference to the System.Windows.interactivity assembly is to create our class that inherits from Behavior as you see below.

image

You will notice that we specify DataForm as the control type for the behavior – this allows us to have a typed property AssociatedObject that is of type DataForm. 

What we want to do is hook into the AutoFieldGeneratingEvent like we did in the past examples.  Behaviors provide an OnAttached method that you can override and accomplish registering the event handler as you can see below.

image

In the event handler we basically use similar logic like we did in the prior example or more fancy if we want but the idea is for fields we want to not show we set the Cancel flag. 

Using the behavior is pretty easy the first thing you need to do is declare a namespace reference for the System.Windows.Interactivity and the library that contains the behavior class as you can see below.

image

Then on the DataForm control instance we can hook up our behavior by using the Interaction.Behaviors markup as you can see below.

image

Behaviors are cool because they can easily attach on to an existing control to add behavior that makes sense.  By attaching on they keep the core control from becoming bloated with features that only get used once and  a while.

If you think this might be useful to you, drop me an e-mail and I will shoot you over a copy of the code.

posted @ Tuesday, August 11, 2009 1:04 AM | Feedback (0)

Saturday, August 08, 2009 #

Interesting Silverlight links – August 8th

http://timheuer.com/blog/archive/2009/08/07/adding-delete-confirmation-in-silverlight-dataform-control.aspx

Tim Heuer talks about confirming a delete from a Dataform control.

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/08/05/silverlight-3-uk-launch-interview-with-ian-ellison-taylor.aspx

Mike Taulty got the chance to talk with Ellison-Taylor about the Silverlight 3 UK Launch.

http://blogs.msdn.com/delay/archive/2009/08/04/scrolling-so-smooth-like-the-butter-on-a-muffin-how-to-animate-the-horizontal-verticaloffset-properties-of-a-scrollviewer.aspx

If you don’t like the blocky movement of the ScrollViewer control, this will show you how to animate it.

http://compiledexperience.com/blog/posts/Silverlight-3-Navigation-and-Google-Analytics

Get your Silverlight page set up with Google Analyitics.

http://blogs.msdn.com/brada/archive/2009/08/03/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-xx-evolving-an-application.aspx

Another tutorial from Brad Abrams about evolving a Silverlight 3/.NET RIA application

http://www.snowball.be/Printing+In+Silverlight+3+Yes+We+Can.aspx

Printing in Silverlight 3? 

posted @ Saturday, August 08, 2009 2:48 PM | Feedback (0)

Can HTML Keep up with other RIA technologies?

One thing for sure there hasn’t been a dull moment in technology for a while.  The ever increasing pace of change can make it almost as challenging as picking stocks to know which technology to choose.

HTML is one of the interesting technologies because it has been the foundation of the web evolution.  Without going back on a history trip, lets just agree its been around for a while.  Last updated with the HTML 4.01 specification back in 1999, and with a new HTML 5 specification in the works.

Recently, there’s been a lot of hype around HTML 5.  In fact almost enough you might think it’s ready to go and fully supported.  But as Philippe Le Hegaret simply stated on the W3C blog “HTML 5 isn’t a standard yet”  The first draft of HTML 5 can be traced back over a year to January 2008.  The most recent draft was just recently published.  One challenge with HTML 5 is not all the players have been at the table.  In fact Microsoft has just recently joined in, up till now it’s been a Google/ Apple show.

What’s the rush for HTML 5?  Simple, HTML hasn’t kept pace with other emerging RIA technologies like Flash and Silverlight.  HTML 5 promises a lot of fancy new features like built in video, audio, and local data storage to name a few that will close the gap.  This has lead some to speculate if HTML 5 could be the death to Flash, Silverlight and other RIA technologies. I wouldn’t call your florist and order flowers for the funerals just yet….

Why you ask?  HTML 5 is in a fight with some very nimble competing technologies.  The RIA space has been fire with new features being released at neck breaking speed. Most RIA technologies like Flash and Silverlight have streamlined their release cycles to 9 months or less because they don’t have to battle it out with a standards committee.  Most of the competing technologies simply depend on the object tag in HTML to host their plug-in and then they manage the plug-in area as they see fit.    That means that Adobe only has to agree with them self for Flash changes, and same for Microsoft with Silverlight.  HTML 5 has two big challenges.  First, getting all the players to approve the new specification.  Second, and probably an even more challenging aspect is to get it implemented in a consistent way in enough users browsers.  Even today, with the push towards standards in IE8 most the time unless you have a real simple page it won’t look the same the first time you run it in the different browsers.  In fact, sometimes they look radically different!

Like movie sequels you can’t help but think are we just trying to make HTML like all the other popular movies instead of letting it do what it was designed to do originally.  By trying to morph it to be closer to RIA technologies do we risk causing even more fragmentation?  I know for me, writing an application that just works in different browsers and even out of the browser is compelling.  Anytime I spend time working on resolving a browser compatibility issue I feel it’s 100% wasted time that I could have spent building something with higher business value.  Am I against standards?  No, I think they have their place, however, the more complex they get the harder it is to get consensus and compliance. 

So what do you think about HTML 5? Should we start a pool of when it will finally be approved? 

posted @ Saturday, August 08, 2009 2:40 PM | Feedback (0)

Wednesday, July 29, 2009 #

RIA Services Staying Logged In

One of the more useful services that comes out of the box with .NET RIA Services is an authentication service.  In fact, if you create an application using the Silverlight Business Application template it comes already wired up for use with the ASP.NET authentication system.  Brad Abrams has a good walkthrough of the basics of using this service here.

As configured when you create your new application using this template the login is not retained from one browser session to the next.  We are in the process of building a training center application and a frequent request from users is to not have to log in each time.  They would like to be able to click “Keep me logged in” and have it be retained for a period of time.  In the rest of this blog post I’m going to walk through one of the ways to enable this using the .NET RIA Services authentication service.

The first step is to modify the LoginWindow to have a check box to allow the user to indicate they want to stay logged in as you can see in the following image.

image

This requires a simple addition to the XAML for the LoginWindow as you can see below.

<StackPanel Style="{StaticResource LoginControlStyle}">
    <TextBlock Text="" Style="{StaticResource LoginTextStyle}"/>
    <CheckBox x:Name="checkStayLoggedIn" Content="Stay logged in 2 weeks"></CheckBox>
</StackPanel>

We chose two weeks because of the type of application you can make this as long or as short as you would like.  Really what we are going to use this for is to decide if we check the persisted property on the Login call.  Previously, our login call looked like the following just passing the user and password. 

_authOp = _authService.Login(this.loginUserNameBox.Text, this.loginPasswordBox.Password);

We are going to change this logic to to set the IsPersited property on the LoginParameters as you can see in the following revised code.

LoginParameters loginParms = new LoginParameters(this.loginUserNameBox.Text,
           this.loginPasswordBox.Password, checkStayLoggedIn.IsChecked.Value, string.Empty);
_authOp = _authService.Login(loginParms);

Now because we said we are going to keep them logged in for 2 weeks, we also need to make a small change server side to the authentication section in the web.config.  Here we are going to add a Forms timeout with a value in minutes.

<authentication mode="Forms">
  <forms timeout="20160"/>
</authentication>

So at this point that all works, however, when we revisit the application it doesn’t know we are already logged in.  In order to know that, we need to add a call to the LoadUser method.  In this particular application in the MainPage loaded event handler we are showing the LoginWindow by default to allow the user to login.  Our revised code now calls the LoadUser async method from the loaded event handler as you can see below.

AuthenticationService _authService = RiaContext.Current.Authentication;

var loadUserOp = _authService.LoadUser();
loadUserOp.Completed += new EventHandler(loadUserOp_Completed);

Finally, when this completes it calls the loadUserOp_Completed event handler and we can check to see if the user is logged in and if not show the LoginWindow

void loadUserOp_Completed(object sender, EventArgs e)
{
    if (!RiaContext.Current.User.IsAuthenticated)
        new LoginWindow().Show();
}

In the case where the user had persisted the login the IsAuthenticated is now set to true and we don’t show the LoginWindow.

posted @ Wednesday, July 29, 2009 1:52 AM | Feedback (0)

Wednesday, July 01, 2009 #

Snapshot of the Silverlight 4 Wishlist

It’s only been about 9 months since Silverlight 2 was released in October 2008, we are nearing the release of Silverlight 3, and already the momentum is building for Silverlight 4.  A thread recently has been started on the Silverlight forum to capture people’s wish list.  You can read the full forum thread here and add on your own wishes.  

I’m preparing to finish writing part of a chapter that talks about “The Road Ahead” for our soon to be released Silverlight 3 book so I had some help pulling together a summary of what people are asking for already as of today.  The following is just a snapshot but thought you might find it interesting if you didn’t have time to read the long forum list – The top 10 or so on the list all had multiple people requesting those items.

Printing support
Right-click context menus
Host HTML content
Better shaders
Better mouse support
Fix WriteableBitmap
Mic support
Better LocalMessage APIs
Better File IO Support
Full 3D w/GPU acceleration
WebCam support
Concurrent release on Linux
Audio streaming
Chrome in OOB applications
Deep linking/Navigation in OOB apps
Reconsider OOB model
Export UI as bitmap
Complete source code for controls
Better Unit Testing
Text right to left
Mobile support
Better tables (merge columns and such)
Copy text from a textblock
Make TCP/IP compatible w/Flash
SQL reports
Expose PixelShader.SetStreamSource()
Improve rendering performance
Tweening/morphing support
Intellisense in XAML for Blend
Animated images
Pop-up Silverlight windows
Faster Silverlight windowless mode
Silverlight as ActiveX control
Better compression
Custom Open/Save file dialogs
Complete DX10 support
Silverlight apps directly installed OOB

posted @ Wednesday, July 01, 2009 11:32 PM | Feedback (0)

Sunday, June 28, 2009 #

Interesting Silverlight Blog Posts June 28th 2009

From The Silverlight Blog: http://team.silverlight.net/announcements/silverlight-ads-on-xbox-live-announced-at-cannes/

Microsoft announced it will bring Interactive Advertising Bureau (IAB) recognized rich media technologies including Silverlight to Xbox LIVE within the next year

From Pete Brown’s Blog: http://tinyurl.com/ljede5

Pete Brown demonstrates how to bind to a UI element, with the example being a character count for a textbox.

From Laurent Bugnion’s Blog: http://geekswithblogs.net/lbugnion/archive/2009/06/16/mvvm-light-toolkit-silverlight-edition-posted.aspx

Laurent Bugnion has put together a toolkit that allows for easier MVVM development in WPF and Silverlight applications.

From Ian Blackburn’s Blog: http://silverlightforbusiness.net/2009/06/23/pushing-data-from-the-server-to-silverlight-3-using-a-duplex-wcf-service/

Ian Blackburn discussed how Silverlight 3 can be used to push data from the server with a Duplex WCF service.

From Nigel Sampson’s Blog: http://compiledexperience.com/Blog/post/Silverlight-3-Behaviors-Double-Click-Trigger.aspx

Nigel shows how to use a Double Click Trigger, as a Silverlight or WCF behavior, within Microsoft Expression Blend.

From Mike Taulty’s Blog: http://tinyurl.com/lx6pq8

Wintellect has published a whitepaper outlining the differences between WPF and Silverlight

From The Silverlight Blog: http://team.silverlight.net/announcements/what-is-expression/

The Silverlight team discusses what Expression actually is and gives a useful history on the conception of each of the products within.

From Tim Heuer’s Blog: http://timheuer.com/blog/archive/2009/06/05/verify-your-silverlight-application-compatibility.aspx

Tim shows the techniques for keeping your Silverlight 2 applications compatible with Silverlight 3 with the upcoming release.

From David Yack’s Blog:
http://blog.davidyack.com/journal/2009/6/28/ria-services-domain-data-source-invalid-property-errors.html

David talks about how to track down invalid property errors when using .NET RIA Services DomainDataSource controls.

posted @ Sunday, June 28, 2009 1:33 PM | Feedback (0)

Wednesday, April 15, 2009 #

Silverlight 3 Means Business

For years there has been debate between applications deployed to the desktop and applications accessed via the web browser. Like political opponents the debate has been fierce and each side evolving over the years. Deployment complexity and richness of the user interface have always been two of the central battles fought. Each technology has made dramatic improvement including things like Click Once to ease deployment of Smart Client's to AJAX to make web applications more responsive and user friendly. Ongoing investments by Microsoft in WPF and ASP.NET AJAX 4 make clear that neither of these concepts will go away in the foreseeable future.

Silverlight however brings to the table a number of unifying concepts that both web and desktop developers should find appealing. Early on, Silverlight was thought by many to be simply a glorified Adobe Flash competitor offering a way to build a video player with the Microsoft tools. The reality, while it may well be a competitor in that space, its potential for being able to build rich business applications is really where it starts to get interesting. Additionally, as you consider the number of .NET capable developers already trained to build applications using similar Microsoft .NET related technologies it becomes clearer how Silverlight traction will begin to outpace competitors in the space at an amazing rate.

The time has arrived for anyone involved in business application architecture and related technology decisions to begin to engage in understanding the changing landscape. This would include not only ISVs, but also corporate developers building internal line of business applications. That's not saying everyone should move today to Silverlight, but should invest in understanding what changes are emerging. By making the investment in learning, you will be well positioned as the application user interface space continues to evolve over the next couple of years.

The introduction of Silverlight 3 beta makes real the commitment toward Silverlight being more than just a media player. Silverlight 3 introduces more business focused controls, more data and business logic connectivity as well as what could turn out to be the real “Click Once" deployment experience.

Silverlight 3 along with Microsoft .NET RIA Services extend the core connectivity plumbing to offer a richer way of extending business data and logic to the client application. As we will discuss in the future these services make it possible to share business logic as appropriate with the Silverlight client while keeping centralized business rules. This goes directly toward addressing and reducing complexity and developer productivity issues of these types of applications.

Traditionally, plug-ins like flash and Silverlight run in a web browser. As the lines between a true desktop application and a web application blur, so does the ability of Silverlight to run outside the browser. Live Mesh offered the first glimpse when it was announced at Microsoft PDC in October 2008 of a Silverlight application out of the browser with Mesh. Silverlight 3 continues that capability allowing a user to choose to install a Silverlight application to their start menu or desktop for easy access. When launched after saving the Silverlight application runs in a standalone process outside the browser window. Unlike competing technologies, this out of browser experience still allows the developer to use the same code and binaries both in and out of the browser. Using local storage resources and Silverlight's support to detect network availability these out of browser applications can also still run offline disconnected from the network. For many, this will be the unifying capability to further bring together desktop and web application development.

It's easy to jump to conclusions that ASP.NET is dead, or WPF is dead and it's important to understand how Silverlight fits in. The reality is, that all three technologies offer unique value propositions for developers building applications. While nobody has a crystal ball to predict the future we can look at how that plays out today. Today, ASP.NET offers applications the broadest deployment across a diverse set of clients. For broad consumer focused applications ASP.NET will continue to be the most viable option. Silverlight will start to be introduced more often in these applications as rich islands offering users specialized functionality. Developers will continue to build WPF applications to take advantage of having full access to desktop devices and the ability to have other desktop specific features. In some cases we will see lite versions of applications built on Silverlight, with full or more desktop feature enabled versions offered in WPF. In fact, frameworks and strategies are starting to emerge to improve the ability to build an application targeted for both Silverlight and WPF.

Join us as we continue to discuss and connect all these concepts into a clearer picture of how you build business applications using Silverlight. We will start with the basics of when to use Silverlight, move on to architecture concepts, and then steadily discuss the core building blocks that are required to put together a business application.

Today, we launched this blog, a new Twitter account and soon a new website that will be supporting a Silverlight 3 book focused on building business applications with Silverlight. Soon we will be releasing more details about the book and how you can get early access.  If you would like to get notified sign up for the book notification list here.

posted @ Wednesday, April 15, 2009 3:31 PM | Feedback (0)