I have a WPF UserControl. The code behind file declares some RoutedUICommand objects which are referenced in the XAML. The application builds and runs just fine. However Expression Blend 3 cannot load the XAML in the designer and gives errors like this one:
The member "ResetCameraCommand" is not recognized or accessible.
The class and the member are both public. Building and rebuilding the project in Blend and restarting Blend hasn't helped. Any ideas what the problem is?
Here are fragments of my XAML ...
<UserControl x:Class="CAP.Visual.CameraAndLightingControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CAP.Visual;assembly=VisualApp"
Height="100" Width="700">
<UserControl.CommandBindings>
<CommandBinding Command="local:CameraAndLightingControl.ResetCameraCommand" Executed="ResetCamera_Executed" CanExecute="ResetCamera_CanExecute"/>
</UserControl.CommandBindings>
....
... and the code behind C#
namespace CAP.Visual
{
public partial class CameraAndLightingControl : UserControl
{
public readonly static RoutedUICommand ResetCameraCommand;
static CameraAndLightingControl()
{
ResetCameraCommand = new RoutedUICommand("Reset Camera", "ResetCamera", typeof(CameraAndLightingControl));
}
Expression Blend does NOT load the code-behind. It actually just loads the XAML. You can always create the Command objects in your UserControl.Resources in XAML. If you create something in code-behind and your XAML references it, Expression Blend won't be able to find it since it just parses XAML.
Before you go saying Blend is broken, this is by design. Commands and similar items should be encapsulated in your design/layout logic which should be in your XAML. If you have custom Commands or custom actions, it's still pretty easy to make them available in your XAML.
I have a C# class file called Command.cs in the MyApp.Commands namespace
public static class AppCommands
{
public static RoutedCommand SendData { get { return _sendDataCommand; } }
private static RoutedCommand _sendDataCommand = new RoutedCommand
(
"Send Data",
typeof(AppCommands),
new InputGestureCollection()
{
new KeyGesture(Key.N, ModifierKeys.Alt)
}
)
}
Then your XAML would include...
<UserControl x:Class="MyApp.Window"
xmlns:c="clr-namespace:TBL.SFDC.Commands">
<UserControl.Resources>
<CommandBinding Command="c:AppCommands.SendData" Executed="SendData_Executed" CanExecute="SendData_CanExecute" />
<UserControl.Resources>
</UserControl>