Silverlight 2.0 Cross Domain Calls

I was just reading Scott Guthries blog post about silverlight 2.0 and I came across a section discussing how Silverlight will be able to do cross domain calls. I have been very curious how they were planning on solving this one without risking the security of silverlight.

It turns out that all you need on the remote domain is an XML policy file that will allow applications to call your domain directly. It seems that this is how flash has been doing it for a while now and silverlight 2.0 will also respect flash policy files.

Flash vs. Silverlight, Indeed

I was just looking at indeed.com and decided to put in Flash vs. Silverlight. The results are pretty interesting:

At the time of this posting flash goes from 0% in May ’05 to about 100% in Sept ’07 while silverlight has gone from 0 at May ’07 to 1,300% in Oct. ’07. I guess we can see that Microsoft has done a good job of getting people excited about Silverlight. Now all they need to do is devliver the goods.

Also, one other interesting thing is how F# creates a graph that sort of looks like the New York skyline:

That seems pretty ironic from a functional language.

Dynamic Templated TreeViewItem in WPF

I struggled with this for a long time for some reason so I decided to post a simple example here for everyone who may be encountering the same thing. The scenario I have here is when you have a treeview with several hardcoded treeview items and at the lowest levels of leafs you have them bound to some data provider. The trick is trying to dynamically bind the treeview items to different data templates based on some criteria.

The real example was to have a label and a text box for the dynamic items based on whether you are trying to edit it or not. The trick turned out to be to wrap your DataTemplate in a control and use styling to dynamically switch it.

 Download Sample Project

<Window

 x:Class=”DynamicTemplatedTreeViewItem.Window1″

 xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;

 xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;

 Title=”Window1″

 Height=”300″

 Width=”551″>

 <Window.Resources>

  <ResourceDictionary>

   <ControlTemplate x:Key=”SelectedTemplate” TargetType=”{x:Type Control}”>

    <Label Content=”{Binding XPath=title}” Foreground=”Red” FontWeight=”Bold”  />

   </ControlTemplate>

   <ControlTemplate x:Key=”DefaultTemplate” TargetType=”{x:Type Control}”>

    <Label Content=”{Binding XPath=title}” />

   </ControlTemplate>

   <DataTemplate x:Key=”template”>

    <Control Focusable=”False”>

     <Control.Style>

      <Style TargetType=”{x:Type Control}”>

       <Setter Property=”Template” Value=”{StaticResource DefaultTemplate}” />                           

        <Style.Triggers>

         <Trigger Property=”IsMouseOver” Value=”True”>

          <Setter Property=”Template” Value=”{StaticResource SelectedTemplate}” />

         </Trigger>

        </Style.Triggers>

       </Style>

      </Control.Style>

     </Control>

    </DataTemplate>

    <XmlDataProvider x:Key=”justnbusiness” Source=”http://www.justnbusiness.com/feed.aspx&#8221; />           

   </ResourceDictionary>

  </Window.Resources>

  <Grid>

  <Grid.ColumnDefinitions>

   <ColumnDefinition/>

   <ColumnDefinition/>

  </Grid.ColumnDefinitions>

  <TreeView

   HorizontalAlignment=”Stretch”

   x:Name=”treeView”

   SelectedValuePath=”description”

   BorderBrush=”#00000000″

   Background=”#00000000″>

   <TreeViewItem

    ItemsSource=”{Binding

     Mode=Default,

     Source={StaticResource justnbusiness},

     XPath=/rss/channel/item}”

    ItemTemplate=”{StaticResource template}”

    Header=”Blog Posts”/>

   </TreeView>

   <Border

    Grid.Column=”1″

    CornerRadius=”5,5,5,5″

    BorderThickness=”1,1,1,1″

    BorderBrush=”#FF000000″

    Padding=”5,5,5,5″

    Margin=”20,20,20,20″>

   <TextBlock

    Text=”{Binding

     Path=SelectedValue,

     ElementName=treeView,

     Mode=Default}”

     TextWrapping=”Wrap”/>

  </Border>

 </Grid>

</Window>

 

Twin Cities Code Camp Spring 2008

I will be giving another talk at the next Twin Cities Code Camp about Visual Studio integration. The event is on April 5th and is free so you should definitely come down and make me sweat with questions that I cannot answer!

http://www.twincitiescodecamp.com/

I plan on talking about visual studio integration in general but I will hopefully show off the current state of the NBusiness 3 plugin as well.

XNA AI Talk

Tomorrow night (Thursday, 2/21/08) is the next XNA Twin Cities User Group meeting and I will be giving a presentation on the MinMax algorithm in Drop In. It should be pretty fun, there will be some pizza and swag (including some xbox games) and a few other speakers talking about some other interesting AI topics. If you’re in the area you should definitely stop in and learn about XNA!

Here is the main link http://www.twincitiesxnausergroup.com/

And be sure to RSVP so we can guage how many people will be there and get the appropriate amount of pizza.

NBusiness 3 Updates

I’ve been working on NBusiness a little bit over the weekend. I’ve decided to create a Curried API using extension methods for the CodeDom to speed along template creation. Here is an example of what I have so far.

CodeMethodReferenceExpression onPropertyChanged =
 new CodeMethodReferenceExpression(null, “OnPropertyChanged”);
 
CodeMemberProperty memberProperty = new CodeMemberProperty();
memberProperty.Name = field.Name.ToPascalCase();
memberProperty.Type = new CodeTypeReference(field.Type);
 
memberProperty.Get()
.Return(memberField.Reference());
memberProperty.Set()
.If(             
    memberField.Reference().NotEqualTo(memberProperty.SetValue()))
.Then(
   memberField.Reference().Assign(
      memberProperty.SetValue()),
      onPropertyChanged.Invoke(memberProperty.Name));
Trust me, as ugly as the above seems it’s still a million times better than doing it all the way it was originally written. Hopefully this will mature and get better over time as well. Here is an example of the code it produces.
private int Aid {
 get {
   return _aid;
}
 set {
   if ((_aid != value)) {
      _aid = value;
      OnPropertyChanged(“Aid”);
    }
}
}
Secondly I have been polishing and simplifying the model framework that comes with NBusiness. I have been working to create an abstract DataAccess API that can easily produce any type of query. Here is an example of how you might insert a new model with the next version of NBusiness.
MockModel model = new MockModel();
model.Name = “testinsert”;
model.Save();
This is as normal and will end up calling into MockModel’s “InsertModel” method which may be implemented like so
protected override void InsertModel(IQuery query)
{
query.Insert().Into(
    MockModel.Database.Table,
    MockModel.Database.Name).Values(“@Name”);
query.Go();
query.Parameter(“@Id”).Direction = ParameterValueDirection.Output;
query.Parameter(“@Name”).Value = Name;
query.Load += loader =>
{
   Id = (int)query.Parameter(“@Id”).Value;
};
}
The DataBase.Table and DataBase.Name members are code generated, static ITable and IColumn fields based on your E# model definition. The query object will be passed up to parent objects and down to child objects and will be passed into an abstract provider which will use it to generate SQL and call your database. The IQuery object actually comes from your IDatabaseProvider and may abstract it to work with different databases. My theory at this point is that you could actually parse the query object anyway you like in the IDatabaseProvider and with a little manual work you could even get it working across a webservice.
Here is my MockDatabaseProvider for my unit tests. The provider interface is much simpler with this new technique.
class MockDatabaseProvider : IDatabaseProvider
{
#region IDatabaseProvider Members
 
 public IQuery Query()
{
    return new SqlQuery();
}
 
 public ITable Table(string name)
{
   return new SqlTable(name);
}
 
 public void Execute(IQuery query)
{
   MethodCall.Calls.Add(new MethodCall(
      “Execute”,
      new { Name = “query”, Value = query }));
 
   SqlQuery sqlQuery = query as SqlQuery;
           
    // Simulate setting the identity parameter after an insert
    IParameterValue id = sqlQuery.Parameters.FirstOrDefault(
      v => v.Name == “@Id”);
   
    if (id!=null && id.Direction == ParameterValueDirection.Output)
    {
      id.Value = 100;
    }
 
    sqlQuery.OnLoad(new MockLoader());
}
 
#endregion
}