views:

190

answers:

2

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));
        }
A: 

Did you copy and paste the error? If so, then it looks like you have a typo someplace. Your code behind and XAML have ResetCameraCommand (with two m's in Command), and your error message says ResetCameraComand (with one m in Command).

Theresa
No I did not copy and paste the error. (Blend doesn't let you select or copy the error text so that its a typo I made when posting this bug.) There is no typo in the C# or XAML code: The app compiles and runs properly in Visual Studio.
Brian Ensink
+1  A: 

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.

Here's how I did it for my app:

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>
masenkablast
Thanks for this answer, it looks promising. But it seems a little odd to me that Blend would be able to use the static commands defined in a different class but not the static commands defined in the code behind class. I will try it and accept is as soon as I get chance!
Brian Ensink
The static commands defined in a different class are already compiled. When you use code-behind, Blend will load the XAML and can fine pre-compiled items. It just (oddly enough) won't load the code-behind. At least that's how it did it for me.
masenkablast