gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:55:11 25 4
gpt4 key购买 nike

我正在尝试从VS2012(Window 7)中的问题“Understanding ICommand implementation without MVVM”运行代码,但出现错误:
“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}"

关于c# - Windows Presentation Foundation(WPF)项目不支持AppCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22305939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com