Syntax Error

Code samples and hints

‘Cannot register duplicate name ‘XXX’ in this scope’ in VS 2010

If you have the problem, that Visual Studio 2010 complains ‘Cannot register duplicate name ‘XXX’ in this scope’, check if you have Resources with a x:Name insted of x:Key-Name:

<SolidColorBrush x:Name="NonWorkingResourceName" Color="Red" />
<SolidColorBrush x:Key="WorkingResourceName" Color="Blue" />

Replace x:Name with x:Key and everything is ok.

Read the full post here ...

Caliburn.Micro – Introduction

Caliburn.Micro – Introduction

Caliburn.Micro is a lightweight and small, yet powerful framework for developing rich WPF, Silverlight or Windows Phone applications. Caliburn.Micro has only a single dependency to the System.Windows.Interactivity library. Due to its small footprint of only 75 k and approximately 2700 lines of code it is not only lightning fast in startup and execution time but also easy to understand. The framework offers pattern guidance for the following well established design patterns:

  • Model – View – ViewModel (MVVM)
  • Model – View – Presenter (MVP)
  • Model – View – Controller (MVC)

Rob Eisenberg (www.robeisenberg.com) and Marco Amendola (marcoamendola.wordpress.com) are actively maintaining the project and taking care of the documentation. The documentation is really thorough and offers tips and tricks how to avoid the common pitfalls. The project is released under a developer and company friendly license namely the MIT License.

http://www.bbv.ch/?p=450&option=com_wordpress&Itemid=173

WPF – Styles & Templates (Gratis-Booklet für Einsteiger)

WPF – Styles & Templates (Gratis-Booklet für Einsteiger)

WPF bietet viele neue Möglichkeiten zur Gestaltung von Controls und deren Anbindung an Datenbestände. Für Einsteiger und Gelegenheitsanwender ist es jedoch immer recht schwierig, sich die richtigen Mittel aus der grossen Palette herauszusuchen und korrekt anzuwenden.

Das vorliegende Booklet beschäftigt sich mit ausgewählten, in WPF zentralen Themen und soll als Nachschlagewerk betrachtet werden. Das Booklet richtet sich somit an Entwickler, die sich neu mit WPF beschäftigen und solche, die noch nicht regelmässig damit gearbeitet haben.

Es handelt sich nicht um eine vollständige Abhandlung aller Möglichkeiten und Lösungswege der jeweiligen Themen, sondern beschränkt sich auf die im Normalfall relevanten Bereiche.

Ausserdem werden in diesem Booklet die weiterführenden, in realen Projekten unerlässlichen Konzepte wie z.B. Model-View-ViewModel (MVVM) und Dependency Injection mit Rücksicht auf die Einfachheit der Codebeispiele nicht einbezogen.
 

Kapitel

  • Übersicht
  • Ressourcen
    • Überblick
    • Statische und dynamische Ressourcen
    • Application
    • Window & Page
    • Controls
    • ResourceDictionary
    • Themes
    • MSBuild Resource-Dateien
    • MSBuild Page-Dateien
    • Lose Dateien
  • Style
    • Überblick
    • Vererbung
    • Styles als Ressourcen
    • Direkte Deklaration
  • Template
    • Überblick
    • ControlTemplate
    • DataTemplate
    • ItemsPanelTemplate
  • Databinding
    • Überblick
    • ElementName-Binding
    • Source-Binding
    • DataContext-Binding
    • RelativeSource
    • TemplateBinding
    • UpdateSourceTrigger
    • Mode
    • Validierung
    • Converter
  • Trigger
  • Glossar

 

Download
WPF - Styles & Templates.pdf (1.07 mb)

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

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.

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

Databinding in WPF

Databinding in WPF

Binding bind = new Binding();
bind.Path = new PropertyPath(TextBox.TextWrappingProperty);
bind.Mode = BindingMode.TwoWay;
wordWrapMenuItemInstance.SetBinding(WordWrapMenuItem.WordWrapProperty, bind);

WPF-Application Shutdown Mode

WPF-Application Shutdown Mode

 

[STAThread]
public static void Main()
{
   Application app = new Application();
   app.ShutdownMode = ShuthdownMode.OnMainWindowClose;
   app.Run(new Mainwindow()); 
}

XAML dynamisch interpretieren

XAML dynamisch interpretieren

string xaml =
   "<Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
   "Foreground='LightSeaGreen' FontSize='24pt'> " +
   "Click me! " +
   "</Button>";


object obj = XamlReader.Load(new XmlTextReader(new StringReader(xaml)));