WPF MVP 命令绑定
也许有人可以向我描述如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论