Rants Tagged with “Silverlight 2”

<<  <  1  2  3  4  5  6  7  8  9  10  11  +  >  >>  (Total Pages: 13/Total Results: 121)

Cool...I am on .NET Rocks TV (DNR-TV)

Silverlight Logo

While in Bulgaria I had time to sit down with Carl Franklin and do an episode of DNRTV.  I am happy to annouce its now live. I built a simple data-driven application using Silverlight 2 with Blend 2 and Visual Studio. This screencast should some basic techniques of separation of UI and Data in Silverlight that will help you do your data binding directly in Blend 2 SP1.  If you haven't seen how this works, go grab the Screencast now!

Using Isolated Storage Settings in Silverlight 2

Silverlight Logo

Here is a quick but fun tip for working with Silverlight 2. I found that many people are using Isolated Storage for saving user preferences or other small pieces of information. When I look at the code, I am surprised by how much trouble they are going through to save small bits of data.  That's where Isolated Storage Settings come in.

For example, let's assume you want to be able to store a FavoriteColor for a user.  You could cruft up a bunch of code to save a file and handle the serialization, or you could use the IsolatedStorageSetting class. This class supports being able to store arbitrary objects in a settings file in Isolated Storage. The IsolatedStorageSettings class supports two keyed collections, ApplicationSettings and SiteSettings. ApplicationsSettings is specific to your .xap file, while SiteSettings are specific to your domain.  Here is an example of a property that uses the IsolatedStorageSettings to store a nullable Color for FavoriteColor:

const string FAVCOLORNAME = "favoriteColor";
public Color? FavoriteColor
{
  get
  {
    if (IsolatedStorageSettings.ApplicationSettings[FAVCOLORNAME] != null)
    {
      Color? colorSetting = 
        IsolatedStorageSettings.ApplicationSettings[FAVCOLORNAME] as Color?;
      if (colorSetting != null) return colorSetting;
    }

    // If we can't find a favorite color, return a null color
    return new Color?();
  }
  set
  {
    IsolatedStorageSettings.ApplicationSettings[FAVCOLORNAME] = value;
  }
}

If you are familiar with ASP.NET, you'll probably see that the pattern here is similar to Cache or Session data.  The idea is the same.  You should always code this defensively as Isolated Storage maybe cleared or could be disabled by the user. In addition, you should be careful *not* to store sensitive data as the user can get access to files in Isolated Storage (though its hidden in a deep directory structure). So avoid saving sensitive data like connection strings or other sensitive information that you want to keep from the user.

HTH

Atlanta Silverlight Tour Stop Almost Here!

Silverlight Logo

There is still time to register if you want to join me in Atlanta for the first Silverlight 2 Release workshop. I will be guiding the students through the maze of skills required for Silverlight 2 including XAML, Blend, Visual Studio 2008, Web Services, ADO.NET Data Services and even advanced topics like control customization and line-of-business application development.

If you're intersted in the class, please register through http://silverlight-tour.com.

Minor Pet Peeve

Silverlight Logo

Not a biggie, but just so that anyone that reads my blog knows...its "Silverlight 2", not "Silverlight 2.0". Wasn't my decision, but its the fact. Journalists and bloggers alike have been throwing out "2.0" a lot lately and after being hammered by some well-meaning Microsofties about the name, I just wanted to make sure everyone knows the right way.

'Nuff about that...

Understanding Image and Media Failed Errors

Silverlight Logo

In meeting with a client (Schoolmaster.nl who is building a cool LOB app), we came across the problem of one of their components was throwing Image Failed Javascript errors. Handling them in the App.UnhandledException event didn't help because the errors were surfaced outside the plug-in, directly to Javascript.  Immediately I use the VisualTreeHelper to walk the entire XAML tree (including nested templates) to just add an event handler on to every ImageFailed events to try and suppress these errors. 

public Page()
{
  InitializeComponent();

  Loaded += (s, e) =>
    {
      // Call Recursive method to wire all Image tags
      WireImageFailed(this);
    };
}

void WireImageFailed(DependencyObject source)
{

  for (int x = 0; x < VisualTreeHelper.GetChildrenCount(source); ++x)
  {
    DependencyObject child = VisualTreeHelper.GetChild(source, x);
    if (child is Image)
    {
      ((Image)child).ImageFailed += (s,e)=>
        {
          // NOOP
        };
    }
    WireImageFailed(child);
  }

}

In case you're not familiar with this class, it helps you walk through the entire render tree at runtime (including inside controls and control templates). This means I could look for all the Image objects and handle the event to suppress the error. This worked but it felt hacky and doesn't work for them as they are building a lot of dynamic XAML.

After digging further, I found out that for compatibility with Silverlight 1.0, they are actually thrown at the Plug-in level so if you want to suppress them, just handle the OnError on the plugin. You can do this no matter how you are hosting the plugin:

<!-- ASP.NET Silverlight Control -->
<asp:Silverlight ID="Xaml1" 
                 runat="server" 
                 Source="~/ClientBin/BadImageTest.xap"
                 MinimumVersion="2.0.31005.0" 
                 Width="100%" 
                 Height="100%" 
                 OnPluginError="SLPluginError" />

<!-- OBJECT Tag -->
<object data="data:application/x-silverlight-2," 
        type="application/x-silverlight-2" 
        width="100%" 
        height="100%">
	<param name="source" 
	       value="ClientBin/BadImageTest.xap"/>
	<param name="minRuntimeVersion" 
	       value="2.0.31005.0" />
	<param name="onerror" 
	       value="SLPluginError" />
</object>

By handling the plugin error, we can either do something about it or just ignore the image/media errors:

function SLPluginError(sender, args) {
  // NOOP
}

Its not elegant, but if you want to stop those annoying script errors from the plug-in, this is the way. At least until I get Microsoft to agree that this isn't a valid way to handle it.

 

Updating My Sites to Silverlight 2 RTW

Silverlight Logo

I've started updating my sites that use Silverlight 2 to the RTW bits. So far I've got silverlight-tour.com and wildermuth.com updated.  SilverlightData.com will have to wait a few days as I have a completely new example that is more complete and dynamically switches between NHibernate and Entity Framework.  Look for an announcement on that this weekend.

The only caveat I have for updating your site is if you're using the Silverlight ASP.NET Control to host your projects. You can copy the SDK to your server if you want but I found that the upgrade to RTW works seemlessly except that I changed the reference to "CopyLocal=True" so I can copy the control to my server instead of expecting it to be installed on the server.

Silverlight 2 Released!

Silverlight Logo

Its been a long road to release but we're finally there. If you're anything like me and have been through WPF/E, Silverlight 1.0, Silverlight 1.1, Silverlight 2 Beta 1, Beta 2 and RC0, I know you're ready for this thing to hit the streets.

I would like to congratulate the team on a solid set of functionality and tooling. For a fairly new product, this technology has progressed much faster than I expected it to. When asked some months ago, I didn't think Silverlight would be in this great a shape until Silverlight 3.  The control model, data binding, data access, tool support, visual state manager, etc. Its all a much bigger leap from Silverlight 1.0 that I could have possibly envisioned.

There are a number of changes since Beta 2 (though this list is the same as the RC) that I want to make sure you know about:

  • Visual Studio 2008 now requires SP1 to be applied before the Silverlight 2 tools will work so make sure you have updated to the Service Pack 1 before you try to install the tool package.
  • Blend 2.5 is now a Service Pack for Blend 2.  You'll need Blend 2 then you can apply Service Pack 1.
  • Rendering is now pixel-aligned by default. In earlier versions you could do sub-pixel rendering which could result in odd or aliased designs.  In the release, all rendering is pixel-aligned unless you override it with FrameworkElement.UseLayoutRounding which is true by default.  Setting it to default will do sub-pixel rendering.
  • For hosting Silverlight with an <OBJECT /> Tag, you must change your type from "application/x-silverlight-2-b2" to "application/x-silverlight-2".
  • For embedded fonts, font URI's must be resource based (instead of XAP based). That means that relative URI's for font's must point to a font-file as a resource inside an assembly instead of as a file inside the .xap file.  If you are using Blend 2 to embed your fonts, this will happen by default.  If you are refactoring old code, changing the font's "Build Action" from "Content" to "Resource" should do the trick.
  • If you are using the VisualStateManager, you will need to change VisualTransition.Duration to VisualTransition.GeneratedDuration.  This usually means changing the "Duration" attribute in your ControlTemplates to "GeneratedDuration". 
  • If you are using the KeyDown event, you'll be happy to find out that the event is now delivered asynchronously.  In Beta 2 and before you could get into re-entrancy issues.  You may need to tweak your use of this event to deal with this new model.
  • OpenFileDialog no longer uses the FileDialogFileInfo class but instead changes the API to simply the use of the selected files.  If you use the dialog, you'll need to adjust the use.
  • Most controls now have a new default control template that should be less Window'y.  If you have made some minor changes to those templates for your own, you will find that they don't match any longer. Because of this you might want to re-skin them in Blend.
  • Finally, the biggest painful change for you is to make sure that your styles and control templates are in the right order. In previous revisions, the rules were lax around StaticResources having to be defined before they are used. This caused a lot of problems for tooling and inconsistent runtime behavior.  To address this you *must* have a related resource defined before its used.  This may mean re-ordering your control templates.

I hope this opens up the more conservative uses of Silverlight 2 and allows Silverlight to move from an 'early-adopter' technology to a mainstream way to build Internet-based applications.

Also announced was the fact that you can use Eclipse now to write Silverlight 2 applications.  Cool!  http://www.eclipse4sl.org/  It'll be licensed under the EPL 1.0 License.

Also announced was the new Controls from Shawn Burk's team to be released on CodePlex!  These controls include:

  • DockPanel
  • WrapPanel
  • ViewBox
  • Label
  • HeaderedContentControl
  • Expander
  •  TreeView
  • NumericUpDown
  • AutoComplete
  • Accordion

These controls and their test-suite will be covered under MS-PL so they will be ready (including source code) coming pretty soon. 

What do you think?

Dirty Little Secrets - Episode 2

Silverlight Logo

My second episode of my "Dirty Little Secrets" screencast is up. This time I show you how to use control templates to skin a complex control in Silverlight 2. I create a ListBox with a Data Template and use ControlTemplates to skin the entire ListBox, Scrollbars, ListBoxItem and all.  Check it out!

You can view the episode online here:

http://wildermuth.com/dls/Dirty_Little_Secrets_Episode_2

Or subscribe to the RSS feed:

Subscribe with RSS
Subscribe with iTunes

Add Shawn Wildermuth&#039;s Dirty Little Secrets Screencast to ODEO 

ADO.NET Data Services and TimeZone

Silverlight Logo

There is a known problem with ADO.NET Data Services today that is important if you (or your server) lives in specific timezones.  The problem is associated with the way that the Silverlight Data Services Library constructs their URI for searches. 

The problem surfaces if you do a query that has a DateTime comparison in it. For example:

var qry = from o in ctx.Orders
          where o.OrderDate <= dt
          select o;

This query generates the following URI in the EST timezone in the US:

http://.../ProductService.svc/Orders()?$filter=OrderDate le datetime'2008-10-13T00:00:00-04:00'

This works great. The problem is that in other timezones (e.g. Bulgaria) where its forward of Greenwich Mean Time, so the UTC date is +03:00 like so:

http://.../ProductService.svc/Orders()?$filter=OrderDate le datetime'2008-10-13T00:00:00+03:00'

Because the "+" isn't URL Encoded, it becomes a space which makes the date incorrect.  For now you can convert the date to universal time but that's a hack at best:

var qry = from o in ctx.Orders
          where o.OrderDate <= DateTime.Today.ToUniversalTime()
          select o;

It works but its a hack.

Silverlight Tour Goes RC

Silverlight Logo

Don't miss your chance to take the first Silverlight Tour stop that is supporting the impending release of Silverlight 2.  Starting with the Atlanta, Georgia - October 22-24th stop of the Silverlight Tour, the course will be taught using Silverlight 2 RC (or the full release once that happens).

The Silverlight Tour is a three-day intensive Silverlight 2 workshop that teaches the basics of designing Silverlight 2 applications including design, development, services, control development, control skinning and security.