从嵌套用户控件 (MVVM) 导出数据
Exporting Data from Nested Usercontrols (MVVM)
我正在寻找一种更好的方法来从模型中获取数据以进行导出。下面我概述了我目前的策略,但我觉得还有更好的策略。
假设我有一个控件 NestingView,带有一个文本框、按钮和 itemscontrol。该按钮将新的 NestingView 添加到 ItemsControl。我的目标是能够导出包括嵌套路径在内的数据。
具体来说,JSON,但我认为这不是重点。不过,作为参考,结果看起来像这样:
{
"Text": "",
"Children": []
}
目前我想出嵌套控件的方法是让 NestingViewModel 包含项目控件使用的 ObservableCollection。那么,保存将是遍历集合中的集合的问题......等等
这,我相信,有效,但是有 VM 有 VM 有 VM 肯定感觉很脏......所以我想知道是否有 better/easier/cleaner/"more 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
}
]
}
but it certainly feels dirty having VMs with VMs with VMs
一点也不,这是世界上最正常的事情。我宁愿说没有 children 的视图模型是不寻常的,除非你有一个非常简单的 wizard-like 应用程序。
此外,在除 proof-of-concept 以外的所有应用程序中,我都不会在视图模型中存储原始数据。始终尝试将数据作为模型存储在某些服务中。视图模型的工作是聚合和处理视图的数据。
我正在寻找一种更好的方法来从模型中获取数据以进行导出。下面我概述了我目前的策略,但我觉得还有更好的策略。
假设我有一个控件 NestingView,带有一个文本框、按钮和 itemscontrol。该按钮将新的 NestingView 添加到 ItemsControl。我的目标是能够导出包括嵌套路径在内的数据。
具体来说,JSON,但我认为这不是重点。不过,作为参考,结果看起来像这样:
{
"Text": "",
"Children": []
}
目前我想出嵌套控件的方法是让 NestingViewModel 包含项目控件使用的 ObservableCollection。那么,保存将是遍历集合中的集合的问题......等等
这,我相信,有效,但是有 VM 有 VM 有 VM 肯定感觉很脏......所以我想知道是否有 better/easier/cleaner/"more 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
}
]
}
but it certainly feels dirty having VMs with VMs with VMs
一点也不,这是世界上最正常的事情。我宁愿说没有 children 的视图模型是不寻常的,除非你有一个非常简单的 wizard-like 应用程序。
此外,在除 proof-of-concept 以外的所有应用程序中,我都不会在视图模型中存储原始数据。始终尝试将数据作为模型存储在某些服务中。视图模型的工作是聚合和处理视图的数据。