I am completely new to MVVM and I am creating an UWP app for keeping track of my software development, I am still learning.
So what I want to make is:
An app that contains single page ->
In MainPage.xaml I have something like this:
<!--MainPage Content-->
<Grid>
<!--For SearchBox-->
<AutoSuggestBox x:Name="SearchBox"/>
<!--For Adding Item-->
<AppBarButton x:Name="AddAppButton"/>
<!--Listview that contains main data-->
<ListView x:Name="AppsListView"/>
<!--This is DataTemplate of listview-->
<DataTemplate>
<Grid>
<!--Icon of App-->
<Image/>
<!--Name of App-->
<TextBlock/>
<!--For Editing Item-->
<AppBarButton/>
<!--For Deleting Item-->
<AppBarButton/>
</Grid>
</DataTemplate>
</Grid>
In Model I have something like this:
public class DevApp
{
public string name { get; set; } // For App Name
public string Iconsource { get; set; } // For App Icon
public ICommand EditCommand; // For Edit AppBarButton
public ICommand DeleteCommand; // For Delete AppBarButton
}
In ViewModel, something like :
public class ViewModel
{
// For ItemSource of ListView
public ObservableCollection<DevApp> DevApps = new ObservableCollection<DevApp>();
// For Add AppBarButton
public ICommand AddCommand;
}
Now this is me first time trying to create a neat and clean Mvvm app. Now I have this question:
- I know how to bind command to button or AppBarButton but how am I supposed to bind a Methods of a Xaml Control such as Listview's SelectionChanged() or AutoSuggestBox's TextChanged() Methods to ViewModel ?
- How can I Load Data from save file ? As there is no InitializeComponent() in ViewModel like in CodeBehind to start from, where shall I pull LoadData() method which loads data to ListView ? ( my viewmodel is bind to view using <MainPage.DataContext> and I wanna keep code behind completely empty. )
- Where shall I put Data class that can manage load save and edit data to savefile.
- How shall I distribute responsibilities among classes ?
I have seen people using mvvm and they create files like:
services, helpers, contracts, behaviours, etc.
and I have seen same thing in Windows Community Toolkit Sample App Is it required for Mvvm. And what are services and helpers. - Shall I really use Mvvm for this ?
I tried using Mvvm in this just for curiosity but like
ITS BEEN 1 MONTH I AM MAKKING THIS APP! but it gets messed up again and again,
If I used Code Behind it would have been done in few days. BY time now I realize that Mvvm is good at data bind in complex apps but
When it comes to simple things like a simple app with listview, I think code-behind
is better and it keeps things simple.
Please answer these questions I am really struggling in making this app.
1条答案
按热度按时间qmb5sa221#
我知道如何将command绑定到button或AppBarButton,但我应该如何将Xaml控件的方法(如Listview的SelectionChanged()或AutoSuggestBox的TextChanged()方法)绑定到ViewModel
您可以使用Xaml Behavior InvokeCommandAction将SelectionChanged与命令绑定,或者使用x:bind标记扩展绑定方法,有关更多信息,请参阅此link。
如何从保存文件中加载数据?由于ViewModel中没有像CodeBehind中那样的InitializeComponent()可供启动,我应该从哪里提取LoadData()方法来将数据加载到ListView?(我的视图模型是使用绑定到视图的<MainPage.DataContext>,我希望保持代码完全为空。
根据第一个问题,你可以检测页面加载事件和
Invoke CommandAction
在视图模型中的位置,然后在视图模型LoadedCommand
中加载文件。我应该把可以管理加载保存和编辑数据Data类放在哪里以保存文件
保存文件的最佳位置是当前应用程序的本地文件夹,并且它具有完全访问权限,请参阅Work with files文档。
我应该如何在类之间分配责任呢?我见过一些人使用mvvm,他们创建了如下文件:服务,助手,合同,行为等,我在Windows社区工具包示例应用程序中也看到了同样的东西。Mvvm需要它吗?什么是服务和助手。
对于mvvm设计,
model view viewmodel
是必要的。并且不需要制作服务、助手、合约、行为,它应该基于您的设计。例如,如果您想要制作NavigateService,您需要制作静态服务类来管理当前应用的导航。我们建议您使用TempleStudio制作包含一些基本服务和行为的示例项目。我真的应该使用Mvvm吗?我试着使用Mvvm只是出于好奇,但就像它已经1个月了,我正在制作这个应用程序!但它一次又一次地搞砸了,如果我使用代码背后,它会在几天内完成。现在我意识到,Mvvm擅长于复杂应用程序中的数据绑定,但当涉及到简单的事情,如一个简单的应用程序与listview,我认为代码隐藏更好,它使事情变得简单。
您的理解是正确的,但是Decoupling(mvvm)您的代码有很多好处,包括: