AppCommand are not supported in a Windows Presentation Foundation (WPF) project

核能气质少年 提交于 2020-01-17 05:21:10

问题


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:

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

回答1:


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"

and use it like so:

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


来源:https://stackoverflow.com/questions/22305939/appcommand-are-not-supported-in-a-windows-presentation-foundation-wpf-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!