gpt4 book ai didi

wpf - MVVM最佳实践与保持简单

转载 作者:行者123 更新时间:2023-12-03 10:49:19 30 4
gpt4 key购买 nike

下面的第一段代码是我很想写的代码。下面的第二个代码部分是先前的代码 worker 在尝试完成相同任务时所写的内容。

先前的同事的代码似乎遵循标准的MVVM惯例,即为每种类型的项目使用单独的ViewModel,在ViewModel中而不是在 View 中跟踪SelectedItems,并在模型中避免使用ObservableCollection。

我很想写的代码大约只有一半的大小和复杂度,模型和ViewModel不同步的可能性较小,而代码行却少得多。

MVVM最佳实践真的是这里的正确答案吗?是否有某种中间立场将两个版本的优点结合在一起?

我的代码:

//Model

public class Cheese
{
public string Name { get; set; }
public int Tastiness { get; set; }
public Color Color { get; set; }
}

public class CheeseEditorModel
{
public ObservableCollection<Cheese> Cheeses { get; private set; }

public CheeseEditorModel()
{
//read cheeses in from file/database/whatever
}

public DeleteCheeses(SelectedObjectCollection selected)
{
//delete cheeses
}
}

//ViewModel

public class CheeseEditorViewModel
{
private CheeseEditorModel _model;
public ObservableCollection<Cheese> Cheeses { get {return _model.Cheeses} }

public CheeseEditorViewModel()
{
_model = new CheeseEditorModel();
}

public DeleteSelected(SelectedObjectCollection selected)
{
_model.Delete(selected);
}
}

//XAML

<ListBox Name="CheeseListBox" ItemsSource={Binding Path="Cheeses"} />
<Button Command={Binding DeleteSelected} CommandParameter="{Binding ElementName=CheeseListBox, Path=SelectedItems}" />

他人代码:
//Model

public class Cheese
{
public string Name { get; set; }
public int Tastiness { get; set; }
public Color Color { get; set; }
}

public class CheeseEditorModel
{
public List<Cheese> Cheeses { get; private set; }

public CheeseDataModel()
{
//read cheeses in from file/database/whatever
}

public DeleteCheeses(IEnumerable<Cheese> toDelete)
{
//delete cheeses
}
}

//ViewModel

public class CheeseViewModel
{
private Cheese _cheese { get; set; }
public bool IsSelected { get; set; }

public CheeseViewModel(Cheese cheese)
{
_cheese = cheese;
IsSelected = false;
}

public string Name {get {return _cheese.Name} set { _cheese.Name = value } }
public int Tastiness {get {return _cheese.Tastiness} set { _cheese.Tastiness= value } }
public Color Color {get {return _cheese.Color} set { _cheese.Color = value } }
}

public class CheeseEditorViewModel
{
private CheeseEditorModel _model;
public ObservableCollection<CheeseViewModel> Cheeses { get; private set; }

public CheeseEditorViewModel()
{
_model = new CheeseEditorModel();
foreach (cheese in _model.Cheeses)
Cheeses.Add(cheese);
}

public DeleteSelected()
{
var selected = from cheese in Cheeses select cheese.CheeseModel where cheese.IsSelected();
_model.Delete(selected);
var selectedVM = from cheese in Cheeses select cheese where cheese.IsSelected();
foreach (cheese in selectedVM)
Cheeses.Remove(selected);
}
}

//XAML

<ListBox ItemsSource={Binding Path="Cheeses"}>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Command={Binding DeleteSelected} />

最佳答案

Cheese实现INotifyPropertyChanged吗?如果是,为什么?但是尽管如此,我还是使用了其他人的密码。您可以轻松添加与模型无关的新内容

关于wpf - MVVM最佳实践与保持简单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11270375/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com