critical expression blend error

I really hate this error:

Its frustrating and deflating. My xaml compiles, runs and looks ok but opening the page in Blend crashes it every single time. Other pages load just fine. Guess I’ll break the page down with comments and find the offending code. My best guess is a style reference..but I wouldn’t know, given the craptastic error shown.
Sure hope this is fixed in Blend 3.

Posted on by daniel
Filed under: Uncategorized | No Comments »

IValueConverter and Silverlight databinding

The code here is based on Silverlight 2 technology.

One of the key features for Silverlight that I’ve made extensive use of lately is the ability to include an IValueConverter in the databinding process. Recently I had an issue with the Silverlight DataGrid and the DataGridTextColumn. I wanted to override FontWeight property of my bound text column but was getting an AG_E PARSER error. Silverlight wasn’t up for having the FontWeight property bound to anything aside from a declarative value. Let me show you the process I worked through to get the right results:

First..the basic non-bold app:



Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace SilverlightValueConversion
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
    }

    public class EmailMessageList : ObservableCollection<EmailMessage>
    {
        public EmailMessageList()
            : base()
        {
           Add(new EmailMessage(”Gordie Howe”, “What happened?”, false));
           Add(new EmailMessage(”Chris Osgood”, “@$###$% @#$##!”, true));
           Add(new EmailMessage(”Gary Bettman”, “Wahoooo!”, false));

        }
    }

    public class EmailMessage
    {
        private string _fromName;
        private string _message;
        private bool _isNew;

        public EmailMessage(string from, string body, bool newmessage)
        {
            this._fromName = from;
            this._message = body;
            this._isNew = newmessage;
        }

        public string FromName
        {
            get {return _fromName;}
            set {_fromName = value;}
        }

        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public bool IsNew
        {
            get { return _isNew; }
            set { _isNew = value; }
        }
    }
}

Page.xaml
<UserControl xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”SilverlightValueConversion.Page”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
    xmlns:app=”clr-namespace:SilverlightValueConversion”
    Width=”400″ Height=”200″>
    <UserControl.Resources>
        <app:EmailMessageList x:Key=”MessageListData”/>
    </UserControl.Resources>
    <Grid x:Name=”LayoutRoot” Background=”Silver”>
        <data:DataGrid x:Name=”dgMessages”
              AutoGenerateColumns=”False”
              ItemsSource=”{Binding Source={StaticResource MessageListData}}”>
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header=”From”
                Binding=”{Binding FromName}” />
                <data:DataGridTextColumn Header=”Message”
                Binding=”{Binding Message}” />
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>


If you look at the data source that I’ve thrown together, you’ll see that there is also a field for IsNew, indicating that the message is fresh and unread. Wouldn’t it be great if we could turn the font of the new messages to BOLD? We’re accustomed to having that functionality in our messaging clients so it would be great if we could duplicate that here. To accomplish in place conversion of bound properties, we’ll use an IValueConverter class. The simple description of the conversion process is that we send in an object, interrogate it and return a different value for binding.
This is the code for modifying the FontWeight property:

    public class FontConverter : IValueConverter
    {
        public object Convert(object value,
                              Type targetType,
                              object parameter,
                              System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                bool isNew = (bool)value;
                return isNew ? FontWeights.Bold : FontWeights.Normal;
            }
            else
            {
                return FontWeights.Normal;
            }
        }

        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }

We send in our bound “IsNew” boolean field and it sends out the appropriate FontWeight enumerated value for binding. Now lets turn our attention to the implementation of the FontConverter.
To expose the class to our xaml code, we add a short entry to our Resources tag:

<UserControl.Resources>

<app:EmailMessageList x:Key=”MessageListData”/>

<app:FontConverter x:Key=”fontconverter”/>

</UserControl.Resources>


And then it’s just a matter of working out the binding:

<data:DataGridTemplateColumn Header=”From”>

<data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<TextBlock Text=”{Binding FromName}”

FontWeight=”{Binding IsNew,

Converter={StaticResource fontconverter}}” />

</DataTemplate>

</data:DataGridTemplateColumn.CellTemplate>

</data:DataGridTemplateColumn>

The converter class is called during the binding process, referencing the StaticResource fontconverter class. The IsNew property becomes a parameter for the call and the output is bound to FontWeight.

Note that we’re using a DataGridTemplateColumn to do the binding now. Couldn’t we just use the previously used DataGridTextColumn and bind to the FontWeight Property? I’m glad you asked.
Let try that:

<
data:DataGridTextColumn

Header=”From”

Binding=”{Binding FromName}”

FontWeight=”{Binding IsNew,

Converter={StaticResource fontconverter}}”>

</data:DataGridTextColumn>


If we add this and run it, we’ll get this very informative message:

So yeah…you’ll want to use the DataGridTemplateColumn instead.

So here’s the complete working code:

Page.xaml
<UserControl xmlns:data=”clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data”  x:Class=”SilverlightValueConversion.Page”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
    xmlns:app=”clr-namespace:SilverlightValueConversion”
    Width=”400″ Height=”200″>
    <UserControl.Resources>
        <app:EmailMessageList x:Key=”MessageListData”/>
        <app:FontConverter x:Key=”fontconverter”/>
    </UserControl.Resources>
    <Grid x:Name=”LayoutRoot” Background=”Silver”>
        <data:DataGrid x:Name=”dgMessages”
              AutoGenerateColumns=”False”
              ItemsSource=”{Binding Source={StaticResource MessageListData}}”>
            <data:DataGrid.Columns>
                <data:DataGridTemplateColumn Header=”From”>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text=”{Binding FromName}”
                               FontWeight=”{Binding IsNew,
                                Converter={StaticResource fontconverter}}” />
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>
                <data:DataGridTemplateColumn Header=”Message”>
                    <data:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text=”{Binding Message}”
                               FontWeight=”{Binding IsNew,
                                Converter={StaticResource fontconverter}}” />
                        </DataTemplate>
                    </data:DataGridTemplateColumn.CellTemplate>
                </data:DataGridTemplateColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>

Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace SilverlightValueConversion
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
    }

    public class EmailMessageList : ObservableCollection<EmailMessage>
    {
        public EmailMessageList()
            : base()
        {
           Add(new EmailMessage(”Gordie Howe”, “What happened?”, false));
           Add(new EmailMessage(”Chris Osgood”, “@$###$% @#$##!”, true));
           Add(new EmailMessage(”Gary Bettman”, “Wahoooo!”, false));

        }
    }

    public class EmailMessage
    {
        private string _fromName;
        private string _message;
        private bool _isNew;

        public EmailMessage(string from, string body, bool newmessage)
        {
            this._fromName = from;
            this._message = body;
            this._isNew = newmessage;
        }

        public string FromName
        {
            get {return _fromName;}
            set {_fromName = value;}
        }

        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public bool IsNew
        {
            get { return _isNew; }
            set { _isNew = value; }
        }
    }

    public class FontConverter : IValueConverter
    {
        public object Convert(object value,
                              Type targetType,
                              object parameter,
                              System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                bool isNew = (bool)value;
                return isNew ? FontWeights.Bold : FontWeights.Normal;
            }
            else
            {
                return FontWeights.Normal;
            }
        }

        public object ConvertBack(object value,
                                  Type targetType,
                                  object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}

We run the complete code to get our working grid:

Obviously the grid could use some styling love, but this should get you started on using the IValueConverter. Keep in mind that you can use this thing for quite a few other binding tasks:

· Dynamically set coloring based on database properties

· Compile DateTime fields into custom descriptors

· Conditional testing and output

· many, many more…

You can download my sample solution file HERE.

Posted on by daniel
Filed under: Uncategorized | 1 Comment »

Quick config for Silverlight in SharePoint 2007

As promised during the Sharepoint User Group presentation on Silverlight in SharePoint:

Configuring SharePoint for Silverlight Development Checklist(’ish)

PreReq’s:

  • Windows 2008 OS installed and running with appropriate roles/features
  • SQL Server installed and runningWSS 3.0(sp’d)
  • Visual Studio 2008(sp’d)
  • VseWSS (1.2 or later):
    http://www.microsoft.com/downloads/details.aspx?FamilyID=7bf65b28-06e2-4e87-9bad-086e32185e68&displaylang=en
  • Create your Web Application and a Test Site.
  • Test that your site works.

I’d recommend backing up your VHD at this point, just in case things go awry. And they will. Especially the night before your demo the technology. Not that I’m speaking from experience or anything.

Silverlight Preparation

  • Install Silverlight Tools and the Toolkit:
    http://www.silverlight.net/GetStarted/
  • Make sure that your IIS installation has the MIME type for the .xap extension:
    Extension: .xap
    MIME type: application/x-silverlight-app
  • GAC the Silverlight .dll *:
    The .dll is most likely located at:
    C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Server
  • use Gacutil or filesystem to get the dll into Windows\assembly

    *don’t forget to do this as Administrator.

Configure the Web.Config file:

Backup your sites web.config file!

Here’s a trick that worked on my new vhd and if it doesn’t work for you, hit the web and

search for ‘configuring sharepoint for silverlight’ and you’ll find the hard .config entries for the complete file. In an attempt to do a quick ramp up of the process, try this:

Open Visual Studio 2008.

Create a new Web Site, targeting the 2.0 Framework:

Open the new sites web.config file and delete the contents.

Copy the entire contents from your SharePoint web.config file to the site’s file.

Save it.

Right click on the site project file and select PropertyPages.

Click on Build and change the targeted framework to 3.5:

Add the next few options to the sections as noted:

<system.web><compilation><assemblies> :

<add assembly=”System.Web.Silverlight, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />

<system.web><pages><controls>:

<add tagPrefix=”asp” namespace=”System.Web.UI.SilverlightControls” assembly=”System.Web.Silverlight, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ />

Copy the config contents over to your SharePoint web.config file, replacing the

contents. Save the file.

Reset IIS.

Your Sharepoint environment should be ready to rock and roll the Silverlight development.

My next post will cover a short Silverlight web part built and deployed to SharePoint. Stay Tuned!

Posted on by daniel
Filed under: Uncategorized | 1 Comment »

Absolutely Amazing Technology

Project Natal is an Xbox 360 add on device that uses a wii-like sensor/camera bar to interact with the real world. no controller is needed. you simply move and talk to the software. Milo, a software product coming to the Xbox is simply amazing. Watch the interaction between the actor and the game:

 

Posted on by daniel
Filed under: Uncategorized | 2 Comments »
continue: