WPF MVP 命令绑定

发布于 2024-12-13 15:34:27 字数 2965 浏览 0 评论 0原文

也许有人可以向我描述如何在 mvp 设计模式中正确组织命令绑定?现在我有这个解决方案: 我有命令类:

public class Commands
{
    private static readonly RoutedUICommand OpenTaxGroupListCommand = new RoutedUICommand("OpenTaxGroupList","OpenTaxGroupList",typeof(Commands));


    public static RoutedUICommand OpenTaxGroupList
    {
        get { return OpenTaxGroupListCommand; }
    }
}

并且在我的应用程序演示者中:

   public ApplicationPresenter(IShell view)
        : base(view)
    {

        var openTaxGroupListBinding = new CommandBinding(Commands.OpenTaxGroupList, OpenTaxGroupList,
                                                         CanOpenTaxGroupList);

        CommandManager.RegisterClassCommandBinding(typeof(Window), openTaxGroupListBinding);
    }
    private void CanOpenTaxGroupList(object sender, CanExecuteRoutedEventArgs e)
    {

        if (_taxGroupListPresenter != null)
        {
            if (View.TabExists(_taxGroupListPresenter))
                e.CanExecute = false;
            else e.CanExecute = true;
        }
        else e.CanExecute = true;
    }
   private void OpenTaxGroupList(object sender, ExecutedRoutedEventArgs e)
    {
        DisplayTaxGroups(e.Parameter as ITaxGroupListView);
    }

问题:如何将命令绑定逻辑与 ApplicationPresenter 类分开?

更新: 我找到了这个解决方案:

  public interface ICommandProvider
{
    IEnumerable<RoutedUICommand> GetCommands();
}

在 ApplicationPresenter:

 public IEnumerable<RoutedUICommand> GetCommands()
    {
        return new Collection<RoutedUICommand>
                   {
                       new OpenTaxGroupListCommand(this, View,_taxGroupListPresenter)
                   };
    }

和我的 RoutedUICommand:

 public OpenTaxGroupListCommand(ApplicationPresenter presenter, IShell view, TaxGroupListPresenter taxGroupListPresenter)
    {
        _presenter = presenter;
        _view = view;
        _taxGroupListPresenter = taxGroupListPresenter;
        var openTaxGroupListBinding = new CommandBinding(Commands.OpenTaxGroupList,Execute,CanExecute);
        CommandManager.RegisterClassCommandBinding(typeof(Window), openTaxGroupListBinding);
    }
 private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
       e.CanExecute = true;
    }
private void Execute(object sender, ExecutedRoutedEventArgs e)
    {
        _presenter.DisplayTaxGroups(e.Parameter as ITaxGroupListView, out _taxGroupListPresenter);
    }

和 View 的 xaml:

<Resources:MenuLinkButton x:Name="btnOpenTaxGroupList" Content="Nodokļa grupas" HorizontalAlignment="Left"  
                Style="{StaticResource menuLinkButton}" ImageSource="{StaticResource taxes16Image}" Command="WtpPresenters:Commands.OpenTaxGroupList" 
                                  CommandParameter="{StaticResource  taxGroupListView}"/>

这是正常的解决方案吗?

Maybe someone can describe me how to properly organize commands binding in mvp design pattern? Now i have this solution:
I have commands class:

public class Commands
{
    private static readonly RoutedUICommand OpenTaxGroupListCommand = new RoutedUICommand("OpenTaxGroupList","OpenTaxGroupList",typeof(Commands));


    public static RoutedUICommand OpenTaxGroupList
    {
        get { return OpenTaxGroupListCommand; }
    }
}

and in my application presenter:

   public ApplicationPresenter(IShell view)
        : base(view)
    {

        var openTaxGroupListBinding = new CommandBinding(Commands.OpenTaxGroupList, OpenTaxGroupList,
                                                         CanOpenTaxGroupList);

        CommandManager.RegisterClassCommandBinding(typeof(Window), openTaxGroupListBinding);
    }
    private void CanOpenTaxGroupList(object sender, CanExecuteRoutedEventArgs e)
    {

        if (_taxGroupListPresenter != null)
        {
            if (View.TabExists(_taxGroupListPresenter))
                e.CanExecute = false;
            else e.CanExecute = true;
        }
        else e.CanExecute = true;
    }
   private void OpenTaxGroupList(object sender, ExecutedRoutedEventArgs e)
    {
        DisplayTaxGroups(e.Parameter as ITaxGroupListView);
    }

Question: how can i separate command binding logic from ApplicationPresenter class?

UPDATE:
I found this solution:

  public interface ICommandProvider
{
    IEnumerable<RoutedUICommand> GetCommands();
}

In ApplicationPresenter:

 public IEnumerable<RoutedUICommand> GetCommands()
    {
        return new Collection<RoutedUICommand>
                   {
                       new OpenTaxGroupListCommand(this, View,_taxGroupListPresenter)
                   };
    }

and my RoutedUICommand:

 public OpenTaxGroupListCommand(ApplicationPresenter presenter, IShell view, TaxGroupListPresenter taxGroupListPresenter)
    {
        _presenter = presenter;
        _view = view;
        _taxGroupListPresenter = taxGroupListPresenter;
        var openTaxGroupListBinding = new CommandBinding(Commands.OpenTaxGroupList,Execute,CanExecute);
        CommandManager.RegisterClassCommandBinding(typeof(Window), openTaxGroupListBinding);
    }
 private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
       e.CanExecute = true;
    }
private void Execute(object sender, ExecutedRoutedEventArgs e)
    {
        _presenter.DisplayTaxGroups(e.Parameter as ITaxGroupListView, out _taxGroupListPresenter);
    }

and View's xaml:

<Resources:MenuLinkButton x:Name="btnOpenTaxGroupList" Content="Nodokļa grupas" HorizontalAlignment="Left"  
                Style="{StaticResource menuLinkButton}" ImageSource="{StaticResource taxes16Image}" Command="WtpPresenters:Commands.OpenTaxGroupList" 
                                  CommandParameter="{StaticResource  taxGroupListView}"/>

Is this normal solution?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文