- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
iv'e 在我的启动项目中全局公开了一个 CompositeCommand
public static class Commands
{
public static readonly CompositeCommand DiceRolledCommand = new CompositeCommand();
}
在我的启动项目引用的 ControlLibrary 中,我有一个 Control 有一个 DelegateCommand ,此控件的每个实例都必须使用全局公开的 DiceRolledCommand 注册它的命令。
这样做的最佳做法是什么:
这里有 3 个想法,其中前 2 个我不喜欢,因为它们有点骇人听闻,您采用一些编程组件 (dp) 并为了您的利益而改变它的用途,导致代码和设计不佳。
1) CompositeCommand 类型的常规 decadency 属性,将使用 DiceRolledCommand 设置 并在其 CallBack 上注册 MyControl 的 DelegateCommand (OnDiceRolledCommand) 。
public class MyControl : Control
{
public DelegateCommand<Tuple<int, int>> OnDiceRolledCommand { get; private set; }
public CompositeCommand GlobalDiceRolledCommand
{
get { return (CompositeCommand)GetValue(GlobalDiceRolledCommandProperty); }
set { SetValue(GlobalDiceRolledCommandProperty, value); }
}
public static readonly DependencyProperty GlobalDiceRolledCommandProperty =
DependencyProperty.Register("GlobalDiceRolledCommand", typeof(CompositeCommand), typeof(MyControl), new UIPropertyMetadata(null,GlobalDiceRolledCommandPropertyChanged));
private static void GlobalDiceRolledCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var myControl= d as MyControl ;
var compoisteCommand = e.NewValue as CompositeCommand;
compoisteCommand.RegisterCommand(myControl.OnDiceRolledCommand);
}
}
<local:MyControl GlobalDiceRolledCommand="{x:Static local:Commands.DiceRolledCommand}"/>
我不喜欢这种方法,因为它是一种操作,其中使用的依赖属性具有复杂的逻辑 setter 。
2) 我也可以做与 (1) 中相同的操作,使用带有附加属性的第三方类,该类将在附加属性的 CallBack 中注册 OnDiceRolledCommand
public static class Commands
{
public static readonly CompositeCommand DiceRolledCommand = new CompositeCommand();
public static ICommand GetRegisterToDiceRolledCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(RegisterToDiceRolledCommandProperty);
}
public static void SetRegisterToDiceRolledCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(RegisterToDiceRolledCommandProperty, value);
}
public static readonly DependencyProperty RegisterToDiceRolledCommandProperty =
DependencyProperty.RegisterAttached("RegisterToDiceRolledCommand", typeof(ICommand), typeof(Commands), new UIPropertyMetadata(null,OnRegisterToDiceRolledCommandProperty);
private static void OnRegisterToDiceRolledCommandProperty(DependencyObject d , DependencyPropertyChangedEventArgs e)
{
var commandToRegister = e.newValue as DelegateCommand;
DiceRolledCommand.RegisterCommand(commandToRegister );
}
}
<local:MyContorl local:Commands.RegisterToDiceRolledCommand="{Binding OnDiceRolledCommand , RelativeSource={RelativeSource Self}}"/>
出于与 1 .. 相同的原因,我也不喜欢这种方法
3) 将复合命令作为参数传递给构造函数,这种方法更好,因为它保持 构造函数中的初始化逻辑应该在什么地方,我只是不知道如何传递 通过 XAML 与承包商的争论,我不确定这是否可能。
public class MyControl : Control
{
public MyControl(CompositeCommand globalDiceRolledCommand)
{
.........
globalDiceRolledCommand.Register(OnDiceRolledCommand);
}
}
<local:MyControl ..... >
Some how pass parameters to contractor in order to create the element in XAML
</local:MyControl>
总结:
A) 关于 (1) 和 (2) 的任何想法。
B) 关于如何完成 3 的想法,以及它是否看起来不错的设计。
C) 完成此场景的任何良好模式。
提前致谢。
最佳答案
每当我使用这样的全局命令时,它们通常在每个库都可以引用的基础结构类库中定义。或者它们在每个模块都可以直接引用的消费核心库中定义。
我在 Code Project article 中写了很多这样的东西 Part 2 here
关于wpf - 将 DelegateCommand 注册到 CompositeCommand 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735830/
我通过 Prism 指导工作,并认为我掌握了他们的大多数通信工具。 命令非常简单,因此很明显,DelegateCommand 将仅用于将 View 与其 Model 连接起来。 当涉及到跨模块通信时,
我有一个有多个选项卡的模块。 Tab1 - 员工详细信息,Tab2 - 员工分配,Tab3 - 员工假期。 该模块应允许用户在多个选项卡中输入详细信息并单击 SaveAll 按钮。问题是employe
iv'e 在我的启动项目中全局公开了一个 CompositeCommand public static class Commands { public static readonly
在 c# 程序集中,我有一个全局 prism CompositeCommand 可以像这样订阅: private static readonly CompositeCommand __myComman
我有一个模块 A,它需要在模块 B 的 ViewModel 中执行代码。所以我在我的基础设施中创建了一个 commandProxy,并在 moduleB 中注册了一个命令。当我在 proxyComma
我是一名优秀的程序员,十分优秀!