Brian Ensink
Brian Ensink

Reputation: 11218

Expression Blend doesn't recognize command objects declared in code behind file

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

Upvotes: 1

Views: 1556

Answers (3)

Bill Lefler
Bill Lefler

Reputation: 127

In many cases you must compile your solution IN BLEND to get it to recognize properties etc. from your classes. I was having this same problem and only when a coworker reminded me to recompile in Blend did I get it to work.

Upvotes: 1

sidney.andrews
sidney.andrews

Reputation: 5256

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>

Upvotes: 1

Theresa
Theresa

Reputation: 3645

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).

Upvotes: 0

Related Questions