Expression Blend 无法识别代码隐藏文件中声明的命令对象

发布于 2024-08-28 12:38:42 字数 1216 浏览 4 评论 0原文

我有一个 WPF 用户控件。文件背后的代码声明了一些在 XAML 中引用的 RoutedUICommand 对象。该应用程序构建并运行得很好。但是,Expression Blend 3 无法在设计器中加载 XAML,并会出现如下错误:

成员“ResetCameraCommand”不是 已识别或可访问。

类和成员都是公共的。在 Blend 中构建和重建项目并重新启动 Blend 没有帮助。有什么想法吗?

这是我的 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>
    ....

以及 C# 背后的代码

namespace CAP.Visual
{
    public partial class CameraAndLightingControl : UserControl
    {
        public readonly static RoutedUICommand ResetCameraCommand;

        static CameraAndLightingControl()
        {
            ResetCameraCommand = new RoutedUICommand("Reset Camera", "ResetCamera", typeof(CameraAndLightingControl));
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-09-04 12:38:42

Expression Blend 不会加载隐藏代码。它实际上只是加载 XAML。您始终可以在 XAML 中的 UserControl.Resources 中创建 Command 对象。如果您在代码隐藏中创建某些内容并且 XAML 引用它,Expression Blend 将无法找到它,因为它只是解析 XAML。

在你说 Blend 被破坏之前,这是设计使然。命令和类似的项目应该封装在您的设计/布局逻辑中,该逻辑应该位于您的 XAML 中。如果您有自定义命令或自定义操作,那么在 XAML 中使用它们仍然非常容易。

以下是我为我的应用程序执行此操作的方法:

我在 MyApp.Commands 命名空间中有一个名为 Command.cs 的 C# 类文件

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

然后您的 XAML 将包括...

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

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>
妞丶爷亲个 2024-09-04 12:38:42

在许多情况下,您必须以混合方式编译解决方案,以使其识别类中的属性等。我遇到了同样的问题,只有当同事提醒我在 Blend 中重新编译时,我才让它工作。

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.

渡你暖光 2024-09-04 12:38:42

您是否复制并粘贴了错误?如果是这样,那么看起来你在某个地方有错字。您的代码背后和 XAML 有 ResetCameraCommand (命令中有两个 m),并且您的错误消息显示 ResetCameraComand (命令中有 1 个 m)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文