簡體   English   中英

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

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

我正在嘗試從VS2012(Window 7)中的問題“ 了解沒有MVVM的ICommand實現 ”中運行代碼,但出現錯誤:“ Windows Presentation Foundation(WPF)項目不支持AppCommand”

在此處輸入圖片說明

那么,什么是錯誤的,或者應該怎么做才能從該問題運行代碼?

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

如果您的AppCommands類是在Namespace.To.AppCommads空間中定義的,則需要在XAML中對其進行聲明:

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

並像這樣使用它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM