【问题标题】:CommandBinding WPF命令绑定 WPF
【发布时间】:2016-05-07 11:00:54
【问题描述】:

在我的简单应用程序中,我使用了CommandCommandBinding。 我的问题是,当我像这样在我的 mainWindow 中声明 Command 时:

<RibbonWindow x:Class="BooksDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:BooksDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="400" Width="600">

<Window.CommandBindings>
    <CommandBinding Command="Close" Executed="OnClose" />
    <CommandBinding Command="local:BooksCommands.ShowBooksList" />
</Window.CommandBindings> 

<Ribbon DockPanel.Dock="Top">
    <Ribbon.QuickAccessToolBar>
        <RibbonQuickAccessToolBar>
            <RibbonButton SmallImageSource="Images/one.png"
                          Command="local:BooksCommands.ShowBook"/>
            <RibbonButton SmallImageSource="Images/list.png" 
                          Command="local:BooksCommands.ShowBooksList"/>
        </RibbonQuickAccessToolBar>
    </Ribbon.QuickAccessToolBar>

    <Ribbon.ApplicationMenu>
        <RibbonApplicationMenu SmallImageSource="Images/books.png" >
            <RibbonApplicationMenuItem Header="Show _Book" />
            <RibbonSeparator />
            <RibbonApplicationMenuItem Header="Exit" Command="Close" />
        </RibbonApplicationMenu>
    </Ribbon.ApplicationMenu>

    <RibbonTab Header="Home">
        <RibbonGroup Header="Clipboard">
            <RibbonButton Command="Paste" Label="Paste"
                LargeImageSource="Images/paste.png" />
            <RibbonButton Command="Cut" SmallImageSource="Images/cut.png" />
            <RibbonButton Command="Copy" SmallImageSource="Images/copy.png" />
            <RibbonButton Command="Undo" LargeImageSource="Images/undo.png" />
        </RibbonGroup>
        <RibbonGroup Header="Show">
            <RibbonButton LargeImageSource="Images/one.png" Label="Book" />
            <RibbonButton LargeImageSource="Images/list.png" Label="Book List" />
            <RibbonButton LargeImageSource="Images/grid.png" Label="Book Grid" />
        </RibbonGroup>
    </RibbonTab>

    <RibbonTab Header="Ribbon Controls">
        <RibbonGroup Header="Sample">
            <RibbonButton Label="Button" />
            <RibbonCheckBox Label="Checkbox" />
            <RibbonComboBox Label="Combo1">
                <Label>One</Label>
                <Label>Two</Label>
            </RibbonComboBox>
            <RibbonTextBox>Text Box</RibbonTextBox>
            <RibbonSplitButton Label="Split Button">
                <RibbonMenuItem Header="One" />
                <RibbonMenuItem Header="Two" />
            </RibbonSplitButton>
            <RibbonComboBox Label="Combo2" IsEditable="False">
                <RibbonGallery SelectedValuePath="Content" MaxColumnCount="1"
                SelectedValue="Green">
                    <RibbonGalleryCategory>
                        <RibbonGalleryItem Content="Red" Foreground="Red" />
                        <RibbonGalleryItem Content="Green" Foreground="Green" />
                        <RibbonGalleryItem Content="Blue" Foreground="Blue" />
                    </RibbonGalleryCategory>
                </RibbonGallery>
            </RibbonComboBox>
        </RibbonGroup>
    </RibbonTab>



    <ListBox DockPanel.Dock="Left" Margin="5" MinWidth="120">
        <Hyperlink Click="OnShowBook">Show Book</Hyperlink>
    </ListBox>
    <TabControl Margin="5" x:Name="tabControl1">
    </TabControl>

</Ribbon>

它告诉我BooksCommands 不存在于命名空间中。

在 BooksCommands 类中我声明了命名空间:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BooksDemo
{
  class BooksCommands
  {
    private static RoutedUICommand showBook;
    public static ICommand ShowBook
    {
        get
        {
            return showBook ?? (showBook = new RoutedUICommand("Show Book",
            "ShowBook", typeof(BooksCommands)));
        }
    }
    private static RoutedUICommand showBooksList;
    public static ICommand ShowBooksList
    {
        get
        {
            if (showBooksList == null)
            {
               showBooksList = new RoutedUICommand("Show Books","ShowBooks",
               typeof(BooksCommands));
               showBook.InputGestures.Add(new KeyGesture(Key.B,Modifierkeys.
               Alt));
            }
            return showBooksList;
        }
    }
}

这是 MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;

namespace BooksDemo{
public partial class MainWindow : RibbonWindow
{
       public MainWindow()
    {
        InitializeComponent();

    }


    private void OnClose(object sender, ExecutedRoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }
}
}

谁能帮帮我。

【问题讨论】:

  • 尝试构建你的项目。或 BooksDemo 命名空间在不同的程序集中?
  • 我正在尝试做一个功能区工具栏,并遵循“Professional WPF with c# and .NET 4.5”一书。我重建项目,我仍然有一些问题。不同程序集中的 BooksDemo 命名空间是什么意思?
  • 我用完整的代码编辑了帖子。

标签: c# wpf xaml namespaces


【解决方案1】:

尝试将您的 BooksCommands 类明确声明为公开的。

此外,Visual Studio 中的 XAML 有时似乎对查找引用感到困惑,并会引发错误。有时这些警告/错误是“假的”,如果您尝试运行您的应用程序而不尝试成功构建它,它们就会消失。我认为这是我还没有弄清楚的 Visual Studio 怪癖。

【讨论】:

  • 当我尝试运行项目时,我得到了这个:`Visual Studio 无法开始调试,因为调试目标“C:\Uers\aymen\Documents\Visual Studio 2015\BooksDemo\BooksDemo \bin\Debug\BooksDemo.exe" 丢失。
  • 仅从代码的角度来看,这有点难以调试。我建议您清理您的项目(也许还删除您的 bin/obj 文件夹),然后尝试再次运行。此外,请确保您正在构建正确的配置。
【解决方案2】:
  1. 公开BooksCommands 类。
  2. &lt;CommandBinding Command="local:BooksCommands.ShowBooksList" /&gt; 替换为&lt;CommandBinding x:Key="CmdShowBooksListKey" Command="{x:Static local:BooksCommands.ShowBooksList}" /&gt;

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多