WPF的Mvvmlight框架,非常好看

WPF的Mvvmlight框架,非常好看。

请添加图片描述
编号:92188668787454852千锤百炼

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您举一个简单的例子来搭建一个WPF MVVM框架,使用MVVMLight框架。 首先,我们需要创建一个新的WPF项目。然后,我们需要添加MVVMLight库的引用。您可以通过NuGet管理器来添加,也可以手动下载并添加引用。 接下来,我们需要创建模型(Model)。模型是表示数据和业务逻辑的类。我们可以创建一个名为“Person”的模型类,包含Name和Age属性,如下所示: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } } ``` 然后,我们需要创建视图模型(ViewModel)类。视图模型是连接视图和模型的桥梁。我们可以创建一个名为“MainViewModel”的视图模型类,继承自MVVMLight框架中的ViewModelBase类。在该视图模型类中,我们需要定义一个ObservableCollection属性,用于存储Person对象,以及一些命令,用于添加和删除Person对象。 ```csharp public class MainViewModel : ViewModelBase { private ObservableCollection<Person> _persons; public MainViewModel() { // 初始化Persons集合 _persons = new ObservableCollection<Person>(); } public ObservableCollection<Person> Persons { get { return _persons; } set { Set(ref _persons, value); } } public ICommand AddPersonCommand { get { return new RelayCommand(() => { // 添加Person对象 _persons.Add(new Person()); }); } } public ICommand DeletePersonCommand { get { return new RelayCommand<Person>((person) => { // 删除Person对象 _persons.Remove(person); }); } } } ``` 接下来,我们需要创建视图(View)。视图是用户界面(UI),我们可以使用XAML编写UI。我们可以创建一个名为“MainWindow”的视图,并在其中添加一些控件,如下所示: ```xaml <Window x:Class="MvvmLightSample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:MvvmLightSample.ViewModel" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <vm:MainViewModel/> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal" Margin="10"> <Button Content="Add" Margin="10" Command="{Binding AddPersonCommand}"/> <Button Content="Delete" Margin="10" Command="{Binding DeletePersonCommand}" CommandParameter="{Binding ElementName=personListBox, Path=SelectedItem}"/> </StackPanel> <ListBox Grid.Row="1" Margin="10" Name="personListBox" ItemsSource="{Binding Persons}" DisplayMemberPath="Name"/> </Grid> </Window> ``` 在该视图中,我们使用了DataBinding将视图和视图模型类连接起来。我们将视图模型类的实例设置为视图的DataContext属性,然后在视图中绑定视图模型类的属性和命令。 最后,我们需要在App.xaml.cs文件中启动应用程序,如下所示: ```csharp public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // 启动MVVMLight框架 ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); SimpleIoc.Default.Register<MainViewModel>(); } } ``` 现在,我们已经完成了一个简单的WPF MVVM应用程序,使用了MVVMLight框架。当我们运行该应用程序时,会显示一个包含两个按钮和一个列表框的窗口。当我们单击“Add”按钮时,会在列表框中添加一个空的Person对象;当我们单击“Delete”按钮时,会删除选定的Person对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值