Syntax Error

Code samples and hints

WPF Model-View-ViewModel für Visual Studio 2008

The Model-View-ViewModel toolkit is intended to introduce the Model-View-ViewModel design pattern for building WPF applications to the broad WPF developer community.

The toolkit includes:

  • A Visual Studio 2008 template (Visual C# Express 2008 also supported)
  • Documentation
    • General introduction to M-V-VM
    • Walkthrough using the VS template
  • A complete WPF application demonstrating the M-V-VM pattern

http://wpf.codeplex.com/Wiki/View.aspx?title=WPF%20Model-View-ViewModel%20Toolkit

Script to generate INSERT statements

This procedure generates INSERT statements using existing data from the given tables and views. Later, you can use these INSERT statements to generate the data. It's very useful when you have to ship or package a database application. This procedure also comes in handy when you have to send sample data to your vendor or technical support provider for troubleshooting purposes. 

http://vyaskn.tripod.com/code.htm#inserts

ROW_NUMBER(), RANK(), and DENSE_RANK()

One of the most handy features introduced in SQL 2005 were the ranking functions; ROW_NUMBER(), RANK(), and DENSE_RANK(). For anyone who hasn’t been introduced to these syntactic gems, here’s a quick rundown:

http://thehobt.blogspot.com/2009/02/rownumber-rank-and-denserank.html

ROW_NUMBER()

Aufsteigende Zeilen-Nummer gemäss ORDER BY, optional beschränkt auf Subsets gegeben durch PARTITION BY.

RANK()

Aufsteigende Zeilen-Wertigkeit/Rang (gleiche Zeileninhalte ergeben gleicher Rang) gemäss ORDER BY, optional beschränkt auf Subsets gegeben durch PARTITION BY. Es können Lücken entstehen, wenn mehrere Zeilen den gleichen Rang haben (Bsp. 1, 2, 2, 4, 5 <-- 3 wird ausgelassen, da 2 zwei Mal vorkommt).

DENSE_RANK()

Identisch zu RANK() mit der Ausnahme, dass keine Lücken in der Nummerierung entstehen (Bsp. 1, 2, 2, 3, 4 <-- inkl. 3)

SQL Server 2005 - Reindex der gesamten Datenbank

USE myDB
GO
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 90)"
GO
EXEC sp_updatestats
GO

SQL SERVER 2005 uses ALTER INDEX syntax to reindex database. SQL SERVER 2005 supports DBREINDEX but it will be deprecated in future versions.

PanicButton - c# Control Class

This article is based on the following two blogs:
http://awatts.co.uk/?tag=panic-button
http://project-megasnake.blogspot.com/2008/11/usb-panic-button.html

I created a c# class to control the PanicButton device.

If started, a BackgroundWorker waits for input from the USB device.
To allow the main application work independent from the device state, every state change or button press are reported as Events.

UsbDevice.cs (6.23 kb)

The code expects the original TenxHID.dll in your project.

 

The USB class can be used similar to this example:

public partial class Form1 : Form
{
    readonly UsbDevice usb = new UsbDevice();

    public Form1()
    {
        InitializeComponent();
        usb.ButtonPressed += ButtonPressed;
        // usb.DeviceConnected += DeviceConnected;
        // usb.DeviceDisconnected += DeviceDisconnected;
        // usb.ListeningEnded += ListeningEnded;
        // usb.ListeningStarted += ListeningStarted;
        // usb.ErrorReport += Error;
    }

    private void ButtonPressed(object source, EventArgs args)
    {
        SystemSounds.Beep.Play();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        usb.BeginListening();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        usb.EndListening();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (usb.IsListening)
        {
            e.Cancel = true;
        }
    }
}

Custom Main Method in WPF Applications

To run a custom Main Method in a WPF Application you can do the following steps:

  • Set the Build Action of App.xaml to Page
  • Create a custom Application class
    Example:
namespace WpfCustomMainMethod 
{ 
     using System.Threading; 
     using System.Windows; 

     public class Program : Application 
     { 
          [System.STAThreadAttribute()] 
          static void Main() 
          { 
               var app = new Program(); 
               app.InitializeComponent(); 
               app.MainWindow = new AppWindow(); 
               app.Run(); 
          } 

          public void InitializeComponent() 
          {
               this.Startup += ProgramStartup; 
          } 

          void ProgramStartup(object sender, StartupEventArgs e) 
          { 
               var splash = new Splash {Topmost = true}; 
               splash.Show(); 
               Thread.Sleep(4000); 
               MainWindow.Show(); 
               Thread.Sleep(1500); 
               splash.Close(); 
          } 
     } 
}
  • Change the Startup Object from the Project to your new Application class
  • Edit the Project File and add the following line of code right after "</ProjectTypeGuids>":
<DesignTime Condition="'$(SolutionPath)'!='' AND Exists('$(SolutionPath)')">true</DesignTime>
  • Change the existing lines of the ApplicationDefinition block to this:
<ApplicationDefinition Condition="'$(DesignTime)'=='true' AND 
'$(BuildingInsideVisualStudio)'!='true' AND
'$(BuildingInsideExpressionBlend)'!='true'" Include="App.xaml"> <Generator>MSBuild:Compile</Generator> <SubType>Designer</SubType> </ApplicationDefinition>

Demo Project:
WpfCustomMainMethod.zip (11.54 kb)

This How-To is based on the following blogs:
http://www.infosysblogs.com/microsoft/2008/09/how_to_write_custom_main_metho.html
http://blogs.msdn.com/expression/archive/2008/04/09/creating-a-wpf-blend-project-that-loads-resources-in-code.aspx
http://learnwpf.com/Posts/Post.aspx?postId=a5643949-ab80-47f9-93c8-f5e8e5782d34

ReSharper and GUI-Testing

If you try to run a test like the following from NUnit, you will find that it does not work.
You get an InvalidOperationException telling you that “The calling thread must be STA, because many UI components require this”.

http://www.hedgate.net/articles/2007/01/08/instantiating-a-wpf-control-from-an-nunit-test/

System.InvalidOperationException: Beim aufrufenden Thread muss es sich um einen STA-Thread handeln, da dies für viele Komponenten der Benutzeroberfläche erforderlich ist.

LINQPad

Tired of querying in antiquated SQL?

Well, you don't have to!  LINQPad lets you interactively query SQL databases in a modern query language: LINQ.  Kiss goodbye to SQL Management Studio!

LINQPad supports everything in C# 3.0 and Framework 3.5:

  • LINQ to Objects
  • LINQ to SQL
  • Entity Framework
  • LINQ to XML

LINQPad is also a great way to learn LINQ: it comes preloaded with 200 examples from the book, C# 3.0 in a Nutshell.  There's no better way to experience the coolness of LINQ and functional programming.

And LINQPad is more than just a LINQ tool: it's a code snippet IDE that instantly executes any C#/VB expression, statement block or program – the ultimate in dynamic development. Put an end to those hundreds of Visual Studio Console projects cluttering your source folder!

Best of all, LINQPad standard edition is free and can run without installation (or with a low-impact setup). The executable is 3MB and is self-updating.

http://www.linqpad.net/

List & Label Log File

Enable Log File Debugging on app.config 

<appSettings>
   <add key="ListLabel DebugLogFilePath" value="c:\combit.log"/>
   <add key="ListLabel EnableDebug" value="1"/>
</appSettings>

Standard-Culture definieren

Der folgende Aufruf definiert die aktuelle System-Sprache als aktive Culture innerhalb der gesamten Applikation.

public partial class App : Application
{
    static App()
    {
        FrameworkElement.LanguageProperty.OverrideMetadata(
             typeof (FrameworkElement),
             new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.Name)));
    }
}

Soll die Culture-Zuweisung nur für ein Window gelten, kann die vereinfachte Zuweisung verwendet werden:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:t="clr-namespace:System.Threading;assembly=System"
    Title="Window1" Height="300" Width="300"
    xml:lang="fr-FR">

Composite Application Guidance for WPF and Silverlight

The Composite Application Guidance for WPF and Silverlight is designed to help you more easily build enterprise-level Windows Presentation Foundation (WPF) client applications. This guidance will help you design and build flexible composite client applications-composite applications use loosely coupled, independently evolvable pieces that work together in the overall application.

The Composite Application Guidance can help you develop your client application in a modular fashion. With this approach, you manage the complexity of a large application by breaking it down into smaller, simpler modules. The modules can evolve independently while working together as a unified application.

This version of the Composite Application Guidance is designed to help you build applications in WPF and Silverlight that have a single code base.

The guidance includes a reference implementation, reusable library code (named the Composite Application Library), documentation, QuickStarts, and hands-on labs.

http://msdn.microsoft.com/en-us/library/dd458809.aspx