【发布时间】:2020-06-11 01:07:38
【问题描述】:
我一直在为 Xamarin 做一门课程,我在 MVVM 部分,所有 xaml.cs 代码都移到 ViewModels 中。
这是课程(第 104、105、106 课):[url]https://www.udemy.com/course/complete-xamarin-developer-course-ios-and-android[/url]
到目前为止,我已经能够理解 MVVM 的概念,但是,这门课程似乎做错了一些事情(我相信),并且以前讲座中的一些代码不在新讲座中(我们' 将暂时不理会这种烦恼),继续前进,我不确定如何正确编码。我也明白我目前正在展示一个简单的用例,所以让我们认为 Post 是一个不应该一遍又一遍地重新创建的巨大对象。
例如我有一个PostDetailPage.xaml(.cs),在xaml中,我目前有以下(注意大部分Mode=TwoWay可能使用不正确):
<ContentPage.Content>
<StackLayout Margin="10">
<Entry x:Name="experienceEntry"
Text="{Binding Experience, Mode=TwoWay}"
Margin="-5"></Entry>
<label x:name="venuename"
text="{binding post.venuename, mode=onetime}"
fontattributes="bold"/>
<label x:name="categoryname"
text="{binding post.categoryname, mode=onetime}"/>
<label x:name="address"
text="{binding post.address, mode=onetime}"/>
<label x:name="coordinatelabel"
text="{binding coordinates, mode=onetime}"/>
<label x:name="distance"
text="{binding post.distance, mode=onetime, stringformat='{0:0}'}"/>
<Button Text="Update"
x:Name="updateButton"
Command="{Binding UpdatePostCommand}"
CommandParameter="{Binding Post}" />
</StackLayout>
</ContentPage.Content>
代码如下:
public partial class PostDetailPage : ContentPage
{
PostDetailViewModel viewModel;
public PostDetailPage(Post selectedPost)
{
InitializeComponent();
viewModel = new PostDetailViewModel(selectedPost);
BindingContext = viewModel;
}
}
然后我有一个不完整的 PostDetailViewModel:
public class PostDetailViewModel : INotifyPropertyChanged
{
public UpdatePostCommand UpdatePostCommand { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public PostDetailViewModel(Post post)
{
UpdatePostCommand = new UpdatePostCommand(this);
SelectedPost = post;
}
//Can this become another ViewModel or ?
private Post selectedPost;
public Post SelectedPost
{
get { return selectedPost; }
set
{
selectedPost = value;
OnPropertyChanged("Post");
}
}
private string experience;
public string Experience
{
get { return experience; }
set
{
experience = value;
UpdatePostObject();
OnPropertyChanged("Experience");
}
}
private void UpdatePostObject() {
Post = new Post() {
Experience = experience,
//many more properties are needed here
//seems like if the solution was to grow,
//this would become unmanagable or something
}
}
private void UpdatePostObject()
{
//This is used to Trigger the change on Post
//I don't think this is the best way (hence my question(s))
SelectedPost = new Post()
{
Experience = experience
};
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public async void Update()
{
await App.Current.MainPage.Navigation.PushAsync(new HistoryPage());
}
}
在课程中,讲师建议当Experience 发生更改(或与Post 相关的任何其他属性)时,我们一次又一次地重新创建Post 对象以触发CanExecute 和关联的@ 987654330@。每次更改后必须一次又一次地重新创建 Post 对象似乎是不正确的,所以最好只更新需要的对象吗?
所以,我要问(或了解)的是......
我是否应该实现一个通用的
PostViewModel,当Experience发生变化时更新它,并以某种方式将ICommand附加到它,如果是这样,这样的东西会是什么样子?当然,如果这不正确,请尽可能指出正确的方向。有没有类似
OnObjectChanged的方法可以使用?我在处理收藏时知道ObservableCollection。
如果还有什么我可以提供的,请告诉我。
谢谢 德里克
【问题讨论】:
标签: c# mvvm xamarin.forms inotifypropertychanged