【问题标题】:Rewiring actions of parent to a child viewmodel将父级的操作重新连接到子视图模型
【发布时间】:2023-03-27 20:27:01
【问题描述】:

所以这是我的剧本。我在顶部有一个工具栏(办公风格),带有按钮。这是托管在外壳中的。其中一些按钮在加载时仅适用于某些子视图模型。理想情况下,我希望将按钮 action.target 在创建时重新定位到子视图模型(我通过设置Action.Target="ActiveItem" 来实现这一点。但这并不能完全解决问题:

a) 当子视图模型关闭并且没有活动项目时,我希望它们重新定位到 Shell 作为目标,以便可以将它们设置为“默认”状态。 b)我注意到,当子视图模型关闭并且作为导体的外壳具有 ActiveItem=null 时,动作中的钩子仍然绑定到最后一个视图模型的活动实例,所以看起来它并没有被处理掉。内存泄漏?

对于如何实现这个场景有什么建议吗?

【问题讨论】:

    标签: caliburn.micro


    【解决方案1】:

    向您的 ShellViewModel 添加一个指向操作目标的属性并在激活/停用内容时更新它怎么样:

    例如

    public class ShellViewModel
    {
        public object ActionTarget
        {
            get { return _actionTarget; }
            set 
            { 
                _actionTarget = value; 
                NotifyOfPropertyChange(() => ActionTarget); 
            }
        }
    
        // Then when the active item changes just update the target:
        public override NotifyOfPropertyChange(string propertyName)
        {
            if(propertyName == "ActiveItem") 
            {
                if(ActiveItem == null) ActionTarget = this;
                else ActionTarget = ActiveItem;
            }
        }
    }
    

    现在绑定到那个:

    <SomeMenu cal:Action.Target="{Binding ActionTarget}" />
    

    不确定这是否可行,但我确信我过去做过类似的事情。 (您可能还必须在您更改 ActiveItem 后更新您的操作之前明确调用 NPC)

    【讨论】:

    • 那行得通。唯一的区别是我没有覆盖 NotifyOfPropertyChange 我将 if 插入到导体内的 OnActionationProcessed
    猜你喜欢
    • 2018-03-17
    • 2019-07-03
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多