WPF 创建命令库

发布于 2024-10-13 06:52:28 字数 1498 浏览 0 评论 0 原文

WPF/XAML 新手,希望为我的应用程序构建命令库。我的应用程序的当前版本是“平面”,这意味着背后的所有代码都驻留在我的 MainWindow.cs 文件中。我决定将内容分离到用户控件中,到目前为止,所有 UI 部分都工作得很好,但是……

既然我已经做到了这一点,那么我在将命令绑定到内容时遇到了麻烦。为了简单起见,我创建了一个新的 WPF 项目,添加了我的菜单控件 (Controls\Menu.xaml) 并在我的 MainWindow.xaml 中引用了它。现在,我已经添加了一个 CommandLibrary 类,但似乎没有任何效果。这是我的文件 ->新命令代码:

public static class MyAppCommands
{

    private static RoutedUICommand _NewItem;

    static MyAppCommands()
    {
        _NewItem = new RoutedUICommand("Create a new project", "NewItem", typeof(MyAppCommands));  
    }

    public static RoutedUICommand NewItem
    {
        get { return _NewItem; }
    }

    private string filePath = null;
    private bool dataChanged = false;

    public static void NewItem_Executed(object sender, ExecutedRoutedEventArgs e)
    {

        if (dataChanged)
        {
            string sf = SaveFirst();
            if (sf != "Cancel")
            {
                ClearState();
            }
        }
        else
        {
            ClearState();
        }
    }

    public static void NewItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    public static void BindCommandsToWindow(Window window)
    {
        window.CommandBindings.Add(new CommandBinding(NewItem, NewItem_Executed, NewItem_CanExecute));

    }

我应该将以下代码放在类中的哪里,因为我的很多命令将使用它们......?

private string filePath = null;
private bool dataChanged = false;

非常感谢!

New to WPF/XAML and looking to build a Command library for my application. Current version of my application is "flat" meaning all the code behind resides in my MainWindow.cs file. I've decided to separate things out into User Controls and all the UI part so far works great but....

I'm having trouble binding my commands to things now that I've done this. To make it simple I created a new WPF project, added my Menu control (Controls\Menu.xaml) and referenced this in my MainWindow.xaml. Now, I've gone ahead and added a CommandLibrary class and can't seem to get anything working. Here is my File -> New command code:

public static class MyAppCommands
{

    private static RoutedUICommand _NewItem;

    static MyAppCommands()
    {
        _NewItem = new RoutedUICommand("Create a new project", "NewItem", typeof(MyAppCommands));  
    }

    public static RoutedUICommand NewItem
    {
        get { return _NewItem; }
    }

    private string filePath = null;
    private bool dataChanged = false;

    public static void NewItem_Executed(object sender, ExecutedRoutedEventArgs e)
    {

        if (dataChanged)
        {
            string sf = SaveFirst();
            if (sf != "Cancel")
            {
                ClearState();
            }
        }
        else
        {
            ClearState();
        }
    }

    public static void NewItem_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    public static void BindCommandsToWindow(Window window)
    {
        window.CommandBindings.Add(new CommandBinding(NewItem, NewItem_Executed, NewItem_CanExecute));

    }

Where do I put the below in my class as a lot of my command will be using them...?

private string filePath = null;
private bool dataChanged = false;

Many Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2024-10-20 06:52:28

我认为您可以从设置一些设计模式(例如 MVC 或 MVVM)中受益。

查看这些框架:

您还可以仅使用几个小帮助程序类来遵循 MVC 或 MVVM,查看网络上的文章,例如这篇文章 MVVM

如果您想要一个轻量级框架,请选中此选项:MVVM Foundation

I think you could benefit from setting up some design patterns like MVC or MVVM.

Check out these frameworks:

You can also follow MVC or MVVM just using a couple small helper classes, check out ariticles around the web, like this one MVVM.

Check this if you want a light-weight framework: MVVM Foundation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文