【问题标题】:Exporting Data from Nested Usercontrols (MVVM)从嵌套用户控件 (MVVM) 导出数据
【发布时间】:2018-09-18 23:05:09
【问题描述】:

我正在寻找一种更好的方法来从模型中获取数据以进行导出。下面我概述了我目前的策略是什么,但我觉得有更好的策略。

假设我有一个控件 NestingView,它带有一个文本框、按钮和项控件。该按钮将新的 NestingView 添加到 ItemsControl。我的目标是能够导出包括嵌套路径在内的数据。

具体来说,对于 JSON,但我认为这无关紧要。不过,作为参考,结果如下所示:

{
    "Text": "",
    "Children": []
}

目前我想出嵌套控件的方法是让 NestingViewModel 包含项目控件使用的 ObservableCollection。那么,保存将是遍历集合中的集合...等的问题。

我相信,这可行,但是虚拟机和虚拟机之间的虚拟机肯定感觉很脏......所以我想知道是否有更好/更容易/更清洁/“更多 MVVM”的方法来做它。

为简洁起见,我在此示例中没有使用模型,但假设它会存在并且包含最终用于导出的经过验证的数据。另外,请注意我使用的是 Prism。

NestingViewModel.cs

public class NestingViewModel : BindableBase
{
    /// <summary>
    /// Initializes a new instance of NestingViewModel
    /// </summary>
    public NestingViewModel()
    {
        NestingViewModels = new ObservableCollection<NestingViewModel>();
        NewNestingView = new DelegateCommand(AddNestingViewModel);
    }

    public ObservableCollection<NestingViewModel> NestingViewModels { get; }

    private String _TextBody;

    /// <summary>
    /// Gets and sets the text body
    /// </summary>
    public String TextBody
    {
        get => _TextBody;
        set => SetProperty(ref _TextBody, value);
    }

    public ICommand NewNestingView { get; }

    /// <summary>
    /// Adds a new NestingViewModel to the collection
    /// </summary>
    private void AddNestingViewModel()
    {
        NestingViewModels.Add(new NestingViewModel());
    }
}

NestingView.xaml

<Border BorderBrush="WhiteSmoke" BorderThickness="5">
<StackPanel Margin="5">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="6*"/>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Grid.Column="0" Text="{Binding TextBody}"/>
        <Button Grid.Column="2" Content="New" Command="{Binding NewNestingView}"/>
    </Grid>
    <ItemsControl Margin="20 5 0 0" ItemsSource="{Binding NestingViewModels}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <local:NestingView/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    </StackPanel>
</Border>

Example

因此,在该示例中,JSON 如下所示:

{
    "Text": "Parent",
    "Children": [
        {
            "Text": "ChildA",
            "Children": null
        },
        {
            "Text": "ChildB",
            "Children": [
                {
                    "Text": "ChildB's ChildA",
                    "Children": null
                },
                {
                    "Text": "ChildB's ChildB",
                    "Children": null
                }
            ]
        },
        {
            "Text": "ChildA",
            "Children": null
        }
    ]
}

【问题讨论】:

  • 那么您是在问为什么您的代码无法正常工作,还是在要求改进代码架构/模式?我会澄清您遇到的问题,并明确说明您希望读者帮助解决什么问题。
  • 寻求改进建议“拥有虚拟机和虚拟机的虚拟机确实感觉很脏......所以我想知道是否有更好/更容易/更清洁/“更多 MVVM”的方法来做到这一点。“我'添加了更清晰的开场白

标签: c# wpf xaml mvvm prism


【解决方案1】:

但是虚拟机和虚拟机之间肯定会让人觉得很脏

一点也不,这是世界上最正常的事情。我宁愿说没有孩子的视图模型是不寻常的,除非你有一个非常简单的类似向导的应用程序。

此外,在除概念验证应用程序之外的所有应用程序中,我都会避免将原始数据存储在视图模型中。始终尝试将数据作为模型存储在某些服务中。视图模型的工作是为视图聚合和处理数据。

【讨论】:

  • 听起来不错,我遇到了许多关于将 VM 存储在其他 VM 中的相互矛盾的观点。唯一让我觉得奇怪的是,它基本上排除了 Prisms 自动布线的用处。是的,数据和业务逻辑都在模型中,这只是一个简化的示例。
  • 自动连接仅适用于“根”视图模型,并且仅当您首先导航视图模型时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2018-06-10
  • 2020-06-28
  • 2012-11-07
  • 2011-11-05
  • 1970-01-01
  • 2012-07-11
相关资源
最近更新 更多