简体   繁体   English

Windows Presentation Foundation(WPF)项目不支持AppCommand

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

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" 我正在尝试从VS2012(Window 7)中的问题“ 了解没有MVVM的ICommand实现 ”中运行代码,但出现错误:“ Windows Presentation Foundation(WPF)项目不支持AppCommand”

在此处输入图片说明

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

If your AppCommands class is defined in Namespace.To.AppCommads namespace, you need to declare it in XAML: 如果您的AppCommands类是在Namespace.To.AppCommads空间中定义的,则需要在XAML中对其进行声明:

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

and use it like so: 并像这样使用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows Presentation Foundation (WPF) 项目不支持网格 - Grid is not supported in a Windows Presentation Foundation (WPF) project Windows Presentation Foundation(WPF)项目不支持控件 - Controls is not Supported in the Windows Presentation Foundation (WPF) Project Windows Presentation Foundation(WPF)项目不支持MyCanvas - MyCanvas is not supported in a Windows Presentation Foundation (WPF) project 为什么我会在xaml中的Windows Presentation Foundation(WPF)项目错误中获得“BoolToRowHeightConverter”? - Why would I get a "BoolToRowHeightConverter is not supported in a Windows Presentation Foundation(WPF) project error in xaml? 程序集中的类型是使用旧版本的blend sdk构建的,并且在windows presentation foundation 4项目中不受支持 - the type from assembly is built with an older version of blend sdk and is not supported in a windows presentation foundation 4 project 在Windows演示基础(WPF)应用程序中实现安全登录机制 - Implement a secure login mechansim in Windows presentation foundation (WPF) application 在 Windows Presentation Foundation WPF C# 中禁用自动鼠标悬停 - Disable automatic MouseHover in Windows Presentation Foundation WPF C# 如何使用 get, set in Windows Presentation Foundation (WPF) - How to use get, set in Windows Presentation Foundation (WPF) Microsoft Windows Presentation Foundation错误 - Microsoft windows presentation foundation error .net 4.5和Windows Presentation Foundation中的用户界面 - user interface in .net 4.5 and windows presentation foundation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM