zoukankan      html  css  js  c++  java
  • Caliburn笔记Presenter与View(视图策略)(wpf框架)

    参考:http://caliburn.codeplex.com/wikipage?title=View%20Strategies&referringTitle=Documentation   

    来看下presenter是怎么与view分开的,calbiburn把view和presenter分的很彻底,view很干净,presenter也很干净,几乎感觉不到他们两有关系.这一点感觉设计的非常不错.与asp.net mvc有异曲同工之处.asp.net mvc通过controller的result去寻早view,presenter则与view也有默认的规则。

    ViewStrategy

    image


    即视图查找策略,即根据Presenter的名字然后与View名字相对应去匹配

    如ContactManager.Presenters.ShellPresenter则会生成以下View匹配规则.只要匹配到一个即可image

    来看下项目View的目录结构,Ok,匹配到第2条

    image

    在DefaultWindowManager有一个代码片段

    var view = EnsureWindow(rootModel, _viewStrategy.GetView(rootModel, null, context));


    现在可以知道View与Presenter的转换过程了.

    自定义视图策略

    image
    除了默认查找规则外,还可以通过元数据标签来实现自定义视图策略.如

    [ViewAttribute(typeof(Window1))]
    [Singleton(typeof(IShellPresenter))]
    public class ShellPresenter : Navigator, IShellPresenter
    {
    }


    处理的代码片段

    var customStrategy = modelType.GetCustomAttributes(typeof(ViewStrategyAttribute), true)
        .OfType<ViewStrategyAttribute>().Where(x => x.Matches(context)).FirstOrDefault();
    
    if (customStrategy != null)
        return customStrategy.GetView(model, displayLocation, context);


     

    视图与WPF


    caliburn提供了View的附加属性,可以通过设置Presenter通过视图策略找到View,然后赋给当前的View

    <ContentControl cal:View.Model="{cal:Resolve info:IQuestionPresenter}" />


    如上Resolve是个自定义的标记扩展,用于在依赖注入容器中寻找,这个应用在这个xaml中使用恰到好处.


    个人认为这部分设计的不错.

  • 相关阅读:
    MFC对话框控件数据提取之DoDataExchange()
    慎用USES_CONVERSION
    Oracle OCP 11G 051(61题版本)答案解析目录
    OCP-1Z0-新051-61题版本-61
    OCP-1Z0-新051-61题版本-60
    OCP-1Z0-新051-61题版本-59
    OCP-1Z0-新051-61题版本-58
    OCP-1Z0-新051-61题版本-57
    OCP-1Z0-新051-61题版本-55
    OCP-1Z0-新051-61题版本-56
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1634562.html
Copyright © 2011-2022 走看看