首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Caliburn.Micro嵌套ViewModels最佳实践

Caliburn.Micro嵌套ViewModels最佳实践
EN

Stack Overflow用户
提问于 2014-09-03 16:59:05
回答 1查看 3.4K关注 0票数 8

这是一个很长的问题,所以请容忍我。

目前,我正在开发一个小工具,旨在帮助我跟踪无数的字符在我的故事。

该工具做了以下工作:

  • 加载当前作为json存储在磁盘上的字符,并将它们存储在一个列表中,该列表通过ListBox显示在Shell中。
  • 如果用户随后打开一个字符,则Shell (即Conductor<Screen>.Collection.OneActive )打开一个从Screen派生的新CharacterViewModel
  • Character获得要通过IEventAggregator消息系统打开的字符。
  • CharacterViewModel还有各种属性,它们是子ViewModels,它们绑定到各种子视图。

这是我的问题:目前我在初始化子ViewModels时手动初始化子ChracterViewModel。但这听起来很可疑,我很确定有更好的方法来做这件事,但我看不出我该怎么做。

以下是CharacterViewModel的代码

代码语言:javascript
运行
复制
/// <summary>ViewModel for the character view.</summary>
public class CharacterViewModel : Screen, IHandle<DataMessage<ICharacterTagsService>>
{
    // --------------------------------------------------------------------------------------------------------------------
    // Fields
    // -------------------------------------------------------------------------------------------------------------------

    /// <summary>The event aggregator.</summary>
    private readonly IEventAggregator eventAggregator;

    /// <summary>The character tags service.</summary>
    private ICharacterTagsService characterTagsService;

    // --------------------------------------------------------------------------------------------------------------------
    // Constructors & Destructors
    // -------------------------------------------------------------------------------------------------------------------

    /// <summary>Initializes a new instance of the <see cref="CharacterViewModel"/> class.</summary>
    public CharacterViewModel()
    {
        if (Execute.InDesignMode)
        {
            this.CharacterGeneralViewModel = new CharacterGeneralViewModel();

            this.CharacterMetadataViewModel = new CharacterMetadataViewModel();
        }
    }

    /// <summary>Initializes a new instance of the <see cref="CharacterViewModel"/> class.</summary>
    /// <param name="eventAggregator">The event aggregator.</param>
    [ImportingConstructor]
    public CharacterViewModel(IEventAggregator eventAggregator)
        : this()
    {
        this.eventAggregator = eventAggregator;
        this.eventAggregator.Subscribe(this);
    }

    // --------------------------------------------------------------------------------------------------------------------
    // Properties
    // -------------------------------------------------------------------------------------------------------------------

    /// <summary>Gets or sets the character.</summary>
    public Character Character { get; set; }

    /// <summary>Gets or sets the character general view model.</summary>
    public CharacterGeneralViewModel CharacterGeneralViewModel { get; set; }

    /// <summary>Gets or sets the character metadata view model.</summary>
    public CharacterMetadataViewModel CharacterMetadataViewModel { get; set; }

    /// <summary>Gets or sets the character characteristics view model.</summary>
    public CharacterApperanceViewModel CharacterCharacteristicsViewModel { get; set; }

    /// <summary>Gets or sets the character family view model.</summary>
    public CharacterFamilyViewModel CharacterFamilyViewModel { get; set; }

    // --------------------------------------------------------------------------------------------------------------------
    // Methods
    // -------------------------------------------------------------------------------------------------------------------

    /// <summary>Saves a character to the file system as a json file.</summary>
    public void SaveCharacter()
    {
        ICharacterSaveService saveService = new JsonCharacterSaveService(Constants.CharacterSavePathMyDocuments);

        saveService.SaveCharacter(this.Character);

        this.characterTagsService.AddTags(this.Character.Metadata.Tags);
        this.characterTagsService.SaveTags();
    }

    /// <summary>Called when initializing.</summary>
    protected override void OnInitialize()
    {
        this.CharacterGeneralViewModel = new CharacterGeneralViewModel(this.eventAggregator);
        this.CharacterMetadataViewModel = new CharacterMetadataViewModel(this.eventAggregator, this.Character);
        this.CharacterCharacteristicsViewModel = new CharacterApperanceViewModel(this.eventAggregator, this.Character);
        this.CharacterFamilyViewModel = new CharacterFamilyViewModel(this.eventAggregator);

        this.eventAggregator.PublishOnUIThread(new CharacterMessage
        {
            Data = this.Character
        });


        base.OnInitialize();
    }

    /// <summary>
    /// Handles the message.
    /// </summary>
    /// <param name="message">The message.</param>
    public void Handle(DataMessage<ICharacterTagsService> message)
    {
        this.characterTagsService = message.Data;
    }
}

为了完成任务,我也给了您一个子ViewModels。其他的不重要,因为它们的结构是相同的,只是执行不同的任务。

代码语言:javascript
运行
复制
/// <summary>The character metadata view model.</summary>
public class CharacterMetadataViewModel : Screen
{
    /// <summary>The event aggregator.</summary>
    private readonly IEventAggregator eventAggregator;

    /// <summary>Initializes a new instance of the <see cref="CharacterMetadataViewModel"/> class.</summary>
    public CharacterMetadataViewModel()
    {
        if (Execute.InDesignMode)
        {
            this.Character = DesignData.LoadSampleCharacter();
        }
    }

    /// <summary>Initializes a new instance of the <see cref="CharacterMetadataViewModel"/> class.</summary>
    /// <param name="eventAggregator">The event aggregator.</param>
    /// <param name="character">The character.</param>
    public CharacterMetadataViewModel(IEventAggregator eventAggregator, Character character)
    {
        this.Character = character;

        this.eventAggregator = eventAggregator;
        this.eventAggregator.Subscribe(this);
    }

    /// <summary>Gets or sets the character.</summary>
    public Character Character { get; set; }

    /// <summary>
    /// Gets or sets the characters tags.
    /// </summary>
    public string Tags
    {
        get
        {
            return string.Join("; ", this.Character.Metadata.Tags);
        }

        set
        {
            char[] delimiters = { ',', ';', ' ' };

            List<string> tags = value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).ToList();

            this.Character.Metadata.Tags = tags;
            this.NotifyOfPropertyChange(() => this.Tags);
        }
    }
}

我已经阅读了Screens, Conductors and CompositionIResult and Coroutines并浏览了其余的文档,但不知怎么我找不到我要找的东西。

//编辑:我应该提到我的代码工作得很好。我只是对它不满意,因为我认为我不太理解MVVM的概念,因此会产生错误的代码。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-20 18:59:36

让一个ViewModel实例化几个子ViewModels是没有问题的。如果您正在构建一个更大或更复杂的应用程序,如果您希望保持代码的可读性和可维护性,这几乎是不可避免的。

在您的示例中,每当您创建一个ViewModels实例时,您都会实例化所有四个子CharacterViewModel。每个子ViewModels都将IEventAggregator作为依赖项。我建议您将这四个子ViewModels作为主CharacterViewModel的依赖项,并通过构造函数导入它们:

代码语言:javascript
运行
复制
[ImportingConstructor]
public CharacterViewModel(IEventAggregator eventAggregator,
                            CharacterGeneralViewModel generalViewModel,
                            CharacterMetadataViewModel metadataViewModel,
                            CharacterAppearanceViewModel appearanceViewModel,
                            CharacterFamilyViewModel familyViewModel)
{
    this.eventAggregator = eventAggregator;
    this.CharacterGeneralViewModel generalViewModel;
    this.CharacterMetadataViewModel = metadataViewModel;
    this.CharacterCharacteristicsViewModel = apperanceViewModel;
    this.CharacterFamilyViewModel = familyViewModel;

    this.eventAggregator.Subscribe(this);
}

因此,您可以将子ViewModel属性上的setter设置为私有。

将您的子ViewModels更改为通过构造函数注入导入IEventAggregator

代码语言:javascript
运行
复制
[ImportingConstructor]
public CharacterGeneralViewModel(IEventAggregator eventAggregator)
{
    this.eventAggregator = eventAggregator;
}

在您的示例中,其中两个子ViewModels在构造函数中传递Character数据的一个实例,这意味着一个依赖项。在这些情况下,我将为每个子ViewModel提供一个公共Initialize()方法,在该方法中设置Character数据并在那里激活事件聚合器订阅:

代码语言:javascript
运行
复制
public Initialize(Character character)
{
    this.Character = character;
    this.eventAggregator.Subscribe(this);   
}

然后在CharacterViewModel OnInitialize()方法中调用此方法:

代码语言:javascript
运行
复制
protected override void OnInitialize()
{    
    this.CharacterMetadataViewModel.Initialize(this.Character);
    this.CharacterCharacteristicsViewModel.Initialize(this.Character);    

    this.eventAggregator.PublishOnUIThread(new CharacterMessage
    {
        Data = this.Character
    });


    base.OnInitialize();
}

对于只通过ViewModels更新Character数据的子EventAggregator,将this.eventAggregator.Subscribe(this)调用保留在构造函数中。

如果页面实际不需要任何子ViewModels才能运行,则可以通过属性导入初始化这些VM属性:

代码语言:javascript
运行
复制
[Import]
public CharacterGeneralViewModel CharacterGeneralViewModel { get; set; }

属性导入直到构造函数完成运行之后才会发生。

我还建议通过构造函数注入来处理ICharacterSaveService的实例化,而不是每次保存数据时都显式地创建一个新实例。

MVVM的主要目的是允许前端设计人员在可视化工具(表达式、混合)和代码器中处理UI的布局,以实现行为和业务,而不相互干扰。ViewModel公开要绑定到视图的数据,在抽象级别描述视图的行为,并经常充当后端服务的中介。

没有一个“正确”的方法去做,而且在某些情况下,它不是最好的解决方案。有时候,最好的解决方案是抛出使用ViewModel的额外抽象层,然后编写一些代码隐藏。因此,尽管它是一个很好的应用程序整体结构,但不要陷入迫使所有东西都符合MVVM模式的陷阱。如果您有一些图形化更复杂的用户控件,在这些控件中使用代码隐藏会更好,那么这就是您应该做的。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25649851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档