Posts Tagged WPF

Use XamlReader.Load for WPF XAML (not XamlServices.Load)

While it’s great that we have a common Xaml stack that works across WPF, WF, and WCF, there are some slight differences in the way that WPF loads XAML that doesn’t work with the simple XamlServices.Load API.

 

For WPF XAML, you should use System.Windows.Markup.XamlReader.Load and System.Windows.Markup.XamlWriter.Save.  This will ensure that your XAML will load and save correctly.

 

Here’s a list of some of the special things that XamlReader.Load does: (this isn’t a full list)

  • Enables Journaling (a WPF navigation feature)
  • Sets special attached DependencyProperties on all DependencyObjects (e.g. BaseUri, XmlnsDictionary, XmlSpace etc…)
  • Ignores x:Uid on property elements.  Early in V3 (before we shipped), there was a bug in the UpdateUid build task that put x:Uid on every XML element including property elements.  XAML doesn’t support properties on properties which means that Uid isn’t support on property elements.  We ignored it in V3 and we ignore it in V4 if you use XamlReader.Load (we actually just set XamlReaderSettings.IgnoreUidOnPropertyElements)
  • Wire up events inside of templates and styles
  • Freeze freezables
  • Uses the Wpf XamlSchemaContext (which you can get from System.Windows.MarkupXamlReader.GetWpfSchemaContext())
    • IProvideValueTarget.TargetProperty returns DependencyProperties instead of PropertyInfos.  Binding and DynamicResource require this.
    • There were several other quirks that we added for compat reasons

 

As you can see, most of this stuff isn’t very generic.  It’s pretty specific to WPF and thus we couldn’t justify putting it in the generic System.Xaml parser.

Tags: , ,

XAML Toolkit CTP – November 2009 is now live!

The XAML Toolkit CTP is now live at the code gallery website

Here’s a guide on how to run the rules in FxCop.

Tags: , , , ,

Running FxCop rules against Silverlight or WPF XAML

Read the README that’s included as part of the XAML Toolkit CTP.

The big thing to note is that FxCop 10 Beta 2 does not currently support Silverlight.  You’ll have to use FxCop 1.36 and force it to run in .NET 4 to get things to work for Silverlight.  If you’re running WPF, FxCop 10 should be fine.  (FxCop 10 RC & RTM wills support Silverlight)

After running FxCop, add the XAML Rule

AddRule

Go to the folder where you installed the XAML Toolkit (C:\Program Files\Microsoft XAML Toolkit\) and add Microsoft.Xaml.Tools.FxCop.dll

XamlToolsFxCopdll

You should see all the XAML rules added in the Rules section:

XamlRules

Add your Silverlight/WPF assembly and hit F5 (or Analyze).  You may get a popup about finding System.Windows.dll.  Have it point to C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0

WPF users can point to PresentationFramework/PresentationCore/WindowsBase in C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0

You may also need to point FxCop towards System.Windows.Controls.dll which can be found in: C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client

SystemWindows

If everything goes smoothly you should see the rules run:

FinalRules

You should be able to do similar things for adding your own custom rules to FxCop as well.

 

 

One of the most common requests I had at PDC was to be able check if your WPF XAML was Silverlight friendly and vice versa.  Over the next few weeks, I’ll try working on some simple rules to validate WPF/Silverlight compatibility and whether you can use the XAML between the two platforms.

Tags: , , , , ,