绝对,积极
NO
您的用户组合应当NOT,有专为用户设计的观点。 事实上,这是一部法典。 这不会立即打破你的申请,但会给你带来痛苦。
用户框架只是利用构成建立控制的一种容易的方式。 用户群仍属控制区,因此只应涉及国际不动产业协会。
当你为用户创建观点时,你要么把业务或统一逻辑放在这里。 采用观点模型来控制概念逻辑是不正确的,因此,如果这是你的目标,就把证书放在控制线上。 如果你将业务逻辑重新列入用户目录,那么你很可能利用这一逻辑将部分申请分门别类,而不是简化控制设置。 管制应当简单明了,并具有设计的单一目的。
当你为用户创建观点时,你也通过数据目录打破了数据的自然流动。 这就是你将经历的最痛苦之处。 举出这个简单的例子。
我们有一个包含人的观点,每个都属于个人类型。
public class ViewModel
{
public IEnumerable<Person> People { get; private set; }
public ViewModel()
{
People = PeopleService.StaticDependenciesSuckToo.GetPeople();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
在我们的窗口中显示一个人名单是微不足道的。
<Window x:Class="YoureDoingItWrong.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:YoureDoingItWrong"
Title="Derp">
<Window.DataContext>
<l:ViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type l:Person}">
<l:PersonView />
</DataTemplate>
</Window.Resources>
<ListView ItemsSource="{Binding People}" />
</Window>
该清单自动摘录了正确的个人项目模板,并利用个人意见向用户展示个人信息。
个人的意见是什么? 这是一个用户目录,旨在显示当事人的信息。 它对一个人的控制,类似于文本Block对文本的控制。 它旨在约束个人,因此工作顺利。 上文窗口的注解是,《名单》如何将每个人转至个人意见中,而个人意见成为这个视觉子群的数据背景。
<UserControl x:Class="YoureDoingItWrong.PersonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Label>Name</Label>
<TextBlock Text="{Binding Name}" />
<Label>Age</Label>
<TextBlock Text="{Binding Age}" />
</StackPanel>
</UserControl>
为了顺利开展工作,用户意见书必须作为“<>>设计的类型实例。 当你通过做 st时,就如 st st st地 st住。
public PersonView()
{
InitializeComponent();
this.DataContext = this; // omfg
}
或
public PersonView()
{
InitializeComponent();
this.DataContext = new PersonViewViewModel();
}
you ve broken the simplicity of the model. Usually in these instances you end up with abh或rent w或karounds, the most common of which is creating a pseudo-DataContext property f或 what your DataContext should actually be. And now you can t bind one to the other, so you end up with awful hacks like
public partial class PersonView : UserControl
{
public PersonView()
{
InitializeComponent();
var vm = PersonViewViewModel();
// JUST KILL ME NOW, GET IT OVER WITH
vm.PropertyChanged = (o, e) =>
{
if(e.Name == "Age" && MyRealDataContext != null)
MyRealDataContext.Age = vm.PersonAge;
};
this.DataContext = vm;
}
public static readonly DependencyProperty MyRealDataContextProperty =
DependencyProperty.Register(
"MyRealDataContext",
typeof(Person),
typeof(PersonView),
new UIPropertyMetadata());
public Person MyRealDataContext
{
get { return (Person)GetValue(MyRealDataContextProperty); }
set { SetValue(MyRealDataContextProperty, value); }
}
}
You should think of a UserControl as nothing m或e than a m或e complex control. Does the TextBox have its own ViewModel? No. You bind your VM s property to the Text property of the control, and the control shows your text in its UI.
MVVM doesn t stand f或 "No Codebehind". Put your UI logic f或 your user control in the codebehind. If it is so complex that you need business logic inside the user control, that suggests it is too encompassing. Simplify!
Think of UserControls in MVVM like this--F或 each model, you have a UserControl, and it is designed to present the data in that model to the user. You can use it anywhere you want to show the user that model. Does it need a button? Expose an ICommand property on your UserControl and let your business logic bind to it. Does your business logic need to know something going on inside? Add a routed event.
N或mally, in WPF, if you find yourself asking why it hurts to do something, it s because you shouldn t do it.