GPU.Net – Target the GPU with .Net Languages

GPU.Net (a commercial product) allows you to write .Net code and target GPU Harware with the addition of a [Kernel] attribute. This attribute also allows you to specify a fallback method if a GPU is not available. Multi-threading code seems to differ but something to keep in mind.

Black-Scholes sample here.

Customising Visual Studios Build

Previously, the way I would hook into a projects/solutions build was to edit the .csproj file to import a build file. It always annoyed me and isn’t immediately clear to people (or me after I come back to a project) how the targets are being injected.

I recently discovered that you can follow certain naming conventions to automagically get your targets imported into the build. Now, I know some people think automagic is even less transparent than explicitly defining something, however the naming is quite straightforward and obvious.

Add a targets file named; [after|before].<SolutionName>.sln.targets to the same directory as your solution file.

The after file is added after the project files and the before file is added before the project files.

This only works for solutions.
As an example

<Target Name="RunPostBuildScripts" AfterTargets="Build">
   <!-- Do Stuff Here -->
</Target>

More Info Here

T4

#ThingsINeverKnew

Visual Studio has am inbuilt code-generator/templating engine!
http://t4talk.codeplex.com/

Can use it to generate compile-time and run-time code, or generate anything template based like config.

EventToCommand Action – MVVM Glue

One of my favourite additions to WPF is the Blend Interactivity library which allows you to decorate behaviour into controls in XAML. This really is the glue that allows you to work around MVVM problems in third-party libraries or the framework itself.

The following is an example of a Trigger and Action, to bind SelectedItems to a ViewModel as it changes. This work around is required as SelectedItems isn’t bindable.

<ListBox Name="ItemsToChoose">
  <i:Interaction.Triggers>
    <!-- Can't bind directly to Selected Items 😦 -->
    <i:EventTrigger EventName="SelectionChanged">
      <Command:ChangePropertyOnTarget
          Target="{Binding}" PropertyName="SelectedItems"
          Value="{Binding SelectedItems, ElementName=ItemsToChoose}" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ListBox

One of the most common scenarios for me is the need to execute a command when a control raises a particular event. For instance double-clicking an item in a ListView. The MVVM Light Toolkit has  a great TriggerAction called EventToCommand which has been a great help, allowing me to redirect an event and turn it into a command as such:

<ListBox Name="list">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
      <Command:EventToCommand
           Command="{Binding Path=DataContext.DoSomethingCommand,ElementName=list}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ListBox>

There are two small short-comings with the original. Firstly that it provides no access to the EventArgs. I have tweaked the original to add an EventArgs which you can use in your bindings of the command parameter. (Source Here)

This example shows the tweaked EventToCommand, passing in the DataContext of the clicked row.

<ListBox Name="list">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDoubleClick">
      <Command:EventToCommand
           Command="{Binding Path=DataContext.DoSomethingCommand,ElementName=list}"
 CommandParameter="{Binding EventArgs.OriginalSource.DataContext, RelativeSource={RelativeSource Self}}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</ListBox>

I have also rectified the second issue; it doesn’t implement ICommandSource so it does not handle RoutedCommands.

ComboBox Text

When you want to implement a required ComboBox, but don’t want to default the selection, the initial state of the ComboBox is empty.

How to display a nice message like “Please Select a Widget”?

I always start by setting the Text property, then rub my head as to why it isn’t displayed! The reason is that there is two ControlTemplates used by ComboBox; an Editable one and a non-Editable one. Only the Editable template uses the Text property.

Once you know this small tidbit, the rest is easy. Set IsEditable to true, Set IsReadOnly to True and set your text.

<ComboBox Text="Select An Item or Else!" IsEditable="True" IsReadOnly="True">
       <ComboBoxItem Content="Test 1"/>
       <ComboBoxItem Content="Test 2"/>
</ComboBox>

Note: if you are using a custom ItemTemplate, the selected item will not appear correctly. You should then set IsEditable to true in a trigger when there is no SelectedItem.

<Style TargetType="ComboBox">
  <Style.Triggers>
    <Trigger Property="SelectedItem" Value="{x:Null}">
	<Setter Property="IsEditable" Value="True" />
    </Trigger>
  </Style.Triggers>					
</Style>

Setting TeamCity variables from build

I stumbled upon a useful feature of TeamCity today. The ability to set TeamCity variables from your build. This can be useful for instance if you create a version number in your build which you wish to use as the build number or label for VCS inside TeamCity.

All you need to do is output a message in a particular format. Here is an example of setting the build number in MSBUILD to the value of a property.

<Message Text="##teamcity[buildNumber '$(AssemblyVersionString)']" />

After this has run in your build, the value of the TeamCity variable %system.build.number% will now be assigned the value of  AssemblyVersionString.