Windows Presentation Foundation(WPF)项目不支持AppCommand [英] AppCommand are not supported in a Windows Presentation Foundation (WPF) project

查看:320
本文介绍了Windows Presentation Foundation(WPF)项目不支持AppCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从以下问题运行代码:了解没有MVVM的ICommand实现"(在VS2012(窗口7)中,但出现错误: "Windows Presentation Foundation(WPF)项目不支持AppCommand"

I am trying to run the code from the question "Understanding ICommand implementation without MVVM" in VS2012 (Window 7) but getting errors: "AppCommand are not supported in a Windows Presentation Foundation (WPF) project"

那么,为了运行该问题的代码,什么是错误的?或者我应该怎么做?

Well, what is wrong or what should I do in order to run the code from that question?

MainWindow.xaml.cs:

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace seWPFUnderstandingICommandWithout_MVVM
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            AppCommands.AnyCommand.CanExecuteChanged += MyEventHandler;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Nothing for the moment
        }
        private void MyEventHandler(object sender, EventArgs e)
        {
            Console.WriteLine("MyEventHandler called");
        }
     }
     public static class AppCommands
     {
         private static ICommand anyCommand = new MyCommand();

         public static ICommand AnyCommand
         {
             get { return anyCommand; }
         }
     }
     public class MyCommand : ICommand
     {
         bool canExecute;

         public void Execute(object parameter)
         {
            Console.WriteLine("Execute called!");
         }
         public bool CanExecute(object parameter)
         {
             Console.WriteLine("CanExecute called!");
             return CanExecuteResult;
         }
         public event EventHandler CanExecuteChanged;

         public bool CanExecuteResult
         {
             get { return canExecute; }
             set
             {
                 if (canExecute != value)
                 {
                     canExecute = value;
                     var canExecuteChanged = CanExecuteChanged;
                     if (canExecuteChanged != null)
                         canExecuteChanged.Invoke(this, EventArgs.Empty);
                 }
             }
         }
     }
}

推荐答案

如果在Namespace.To.AppCommads名称空间中定义了AppCommands类,则需要在XAML中声明它:

If your AppCommands class is defined in Namespace.To.AppCommads namespace, you need to declare it in XAML:

xmlns:local="clr-namespace:Namespace.To.AppCommads"

并像这样使用它:

Command="{x:Static local:AppCommands.AppCommand}"

这篇关于Windows Presentation Foundation(WPF)项目不支持AppCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆