【问题标题】:ReactiveUI, get a list from a viewmodel when navigated to - if the viewmodel implements interfaceReactiveUI,导航到时从视图模型中获取列表 - 如果视图模型实现接口
【发布时间】:2014-01-29 08:17:18
【问题描述】:

我正在使用 ReactiveUI 及其路由器。

我的一些IRoutableViewModels 实现了一个接口IHaveCommands,它有一个属性IEnumerable<IReactiveCommand> Commands { get; }

所以,我想要的是我的IScreen 实现AppBootstrapper 中当前视图模型的命令列表。

什么是“正确”的实现方式?

我已经让它与以下代码一起工作,但我的直觉告诉我还有其他更好的方法来做到这一点......

public class AppBootstrapper :  ReactiveObject, IScreen
{
    public IRoutingState Router { get; private set; }

    public AppBootstrapper(IMutableDependencyResolver dependencyResolver = null, IRoutingState testRouter = null)
    {
        Router = testRouter ?? new RoutingState();

        Router.CurrentViewModel.Where(x => x != null)
               .Select(x => typeof(IHaveCommands).IsAssignableFrom(x.GetType()) ? ((IHaveCommands)x).Commands : null)
               .ToProperty(this, x => x.Commands, out _toolbarCommands);

        ...
    }

    readonly ObservableAsPropertyHelper<IEnumerable<CommandSpec>> _toolbarCommands;
    public IEnumerable<CommandSpec> ToolbarCommands { get { return _toolbarCommands.Value; } }

}

有什么建议吗?

【问题讨论】:

    标签: c# system.reactive reactiveui


    【解决方案1】:

    这对我来说看起来很棒!除了一些可能的小可读性修正之外,这是你应该做的™。这是一个稍微干净一点的版本:

    Router.CurrentViewModel
        .Select(x => {
            var haveCmds = x as IHaveCommands;
            return haveCmds != null ? haveCmds.Commands : null;
        })
        .ToProperty(this, x => x.Commands, out _toolbarCommands);
    

    【讨论】:

    • 太棒了!也许我开始明白这件事了:-)
    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 2018-04-26
    • 2023-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多