考完研来公司实习一段时间,初学C#及MVVM,今日予以小记。
庚子年腊月初二
整体文件结构如下:
建立数据库,添加相关数据:
Person.cs代码://建立底层对应于数据库的数据模型
using System;
using System.Collections.Generic;
using System.Text;
namespace rereview.Models
{
//Underlying data model
public class PersonModel
{
public int ID { get; set; }
public string NAME { get; set; }
public int AGE { get; set; }
public string Sex { get; set; }
}
}
PersonService.cs代码://与数据库建立链接及操作数据库
using System;
using System.Collections.Generic;
using System.Text;
using rereview.Models;
using System.Data;
using System.Data.SQLite;
using Dapper;
using System.Linq;
namespace rereview.Services
{
//define common interface
public interface IPersonService
{
IList<PersonModel> GetAll();
}
public class PersonService:IPersonService
{
//DB connect and operate
IDbConnection conn;//System.Data
//operate
public IList<PersonModel> GetAll()
{
string sql = "select * from Person";
var ls = conn.Query<PersonModel>(sql);//Dapper
return ls.ToList();//System.Linq
}
//connect
public PersonService()
{
string connectionStr = "data source=E:\\demo.db";
conn = new SQLiteConnection(connectionStr);//System.Data.SQLite
}
}
}
App.xaml.cs代码://PersonViewModel中需使用到container容器
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Unity;//容器,将方法全项目可用
using rereview.Services;
namespace rereview
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public static UnityContainer container;//Unity
//rewrite OnStartup
protected override void OnStartup(StartupEventArgs e)
{
//formly register to Unity
container = new UnityContainer();
container.RegisterType<IPersonService, PersonService>();
base.OnStartup(e);
}
}
}
PersonViewModel.cs代码://承前启后
using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Mvvm;
using DevExpress.Mvvm.DataAnnotations;
using System.Collections.ObjectModel;
using rereview.Models;
using rereview.Services;
using System.Threading.Tasks;
using Unity;
namespace rereview.ViewModels
{
public class PersonViewModel: ViewModelBase//DevExpress.Mvvm
{
public ObservableCollection<PersonModel> Persons//System.Collections.ObjectModel
{
//Set the Person property to be returned
get { return this.GetValue<ObservableCollection<PersonModel>>(nameof(Persons)); }
set { this.SetValue<ObservableCollection<PersonModel>>(value,nameof(Persons)); }
}
[Command]//DevExpress.Mvvm.DataAnnotations
public async void Query()
{
var res = await Task<IList<PersonModel>>.Factory.StartNew(() =>
{
var ls = service.GetAll();
return ls;
});
//formal assignment
Persons = new ObservableCollection<PersonModel>(res);
}
//Parses the contents of the container and assigns them to the property service
private IPersonService service => App.container.Resolve<IPersonService>();
}
}
index.xaml代码://前端页面展示
<UserControl x:Class="rereview.Views.index"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:rereview.Views"
xmlns:vm="clr-namespace:rereview.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<vm:PersonViewModel/>
</UserControl.DataContext>
<Grid>
<!--Row defination-->
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--Binding the button with the QueryCommand in PersonViewModel-->
<Button Content="刷新" Height="30" Command="{Binding QueryCommand}"></Button>
<!--省略Grid.Row=0-->
<!--Binding the properties defined in PersonViewModel-->
<ItemsControl Grid.Row="1" ItemsSource="{Binding Persons}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="green"></Setter>
<Setter Property="FontSize" Value="25"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
</Style>
</StackPanel.Resources>
<TextBlock Text="{Binding NAME}"></TextBlock>
<TextBlock Text="{Binding AGE}"></TextBlock>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
最后App.xaml中修改启动页面:
<Application x:Class="rereview.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:rereview"
StartupUri="Views/index.xaml">
<Application.Resources>
</Application.Resources>
</Application>
展示效果: