WPF引入sqlsugar

app.xaml.cs

 SugarIocServices.AddSqlSugar(new IocConfig()
            {
                ConnectionString = @"DataSource=" + Environment.CurrentDirectory + @"\DataBase\DeviceSystem.sqlite3",
                DbType = IocDbType.Sqlite,
                IsAutoCloseConnection = true,

            }) ;  

新建一个基础类

 public class BaseRespository<TEntity> : SimpleClient<TEntity>, IRespositoryBase<TEntity> where TEntity : class,new()
    {
        public BaseRespository(ISqlSugarClient sqlSugarClient = null) : base(sqlSugarClient)
        {
            if (sqlSugarClient == null)
            {
                base.Context = DbScoped.Sugar;
                base.Context.DbMaintenance.CreateDatabase();
                base.Context.CodeFirst.InitTables(
                    typeof(TaksInfo),
                    typeof(CaseInfo),
                    typeof(UserInfo),
                    typeof(StepInfo),
                    typeof(StepParam),
                    typeof(AxisInfo),
                    typeof(CameraInfo),
                    typeof(PointInfo)

                    
                    
                    );

            
            
            }

        }
    }

其他类继承这个类就行了

public class TaskInfoRespository:BaseRespository<TaksInfo>,ITaskInfoRespository
{}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF是一种基于XAML的用户界面框架,用于创建桌面应用程序。SQLSugar是一个ORM框架,可以帮助开发人员更轻松地访问和管理数据库。HandyControl是一个WPF控件库,提供了一些常用的界面控件和样式。MVVM是一种设计模式,用于将应用程序的用户界面和业务逻辑分离开来,使得代码更加清晰易懂。 在WPF应用程序中使用SQLSugar可以帮助您更方便地访问和管理数据库数据。使用HandyControl可以快速地创建漂亮的用户界面,并且这些控件都可以轻松地与MVVM模式集成。分页功能可以帮助您将大量数据分成多个页面,使得用户可以更方便地浏览和查找数据。 下面是一个使用WPFSQLSugar、HandyControl和MVVM分页的示例: 1. 创建一个WPF应用程序,并在项目中安装SQLSugar和HandyControl包。 2. 创建一个名为“Models”的文件夹,并在其中创建一个名为“Person.cs”的类,用于定义人员模型。 ```csharp public class Person { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } [SugarColumn(Length = 50)] public string Name { get; set; } [SugarColumn(Length = 50)] public string Gender { get; set; } [SugarColumn(ColumnName = "BirthDate")] public DateTime? DateOfBirth { get; set; } [SugarColumn(Length = 50)] public string Address { get; set; } } ``` 3. 创建一个名为“ViewModels”的文件夹,并在其中创建一个名为“PersonViewModel.cs”的类,用于定义人员视图模型。 ```csharp public class PersonViewModel : BaseViewModel { private int _pageIndex = 1; private int _pageSize = 10; private int _totalCount; private List<Person> _personList; private Person _selectedPerson; public List<Person> PersonList { get { return _personList; } set { SetProperty(ref _personList, value); } } public Person SelectedPerson { get { return _selectedPerson; } set { SetProperty(ref _selectedPerson, value); } } public int PageIndex { get { return _pageIndex; } set { SetProperty(ref _pageIndex, value); } } public int PageSize { get { return _pageSize; } set { SetProperty(ref _pageSize, value); } } public int TotalCount { get { return _totalCount; } set { SetProperty(ref _totalCount, value); } } public ICommand FirstPageCommand { get; private set; } public ICommand PreviousPageCommand { get; private set; } public ICommand NextPageCommand { get; private set; } public ICommand LastPageCommand { get; private set; } public PersonViewModel() { FirstPageCommand = new RelayCommand(FirstPage); PreviousPageCommand = new RelayCommand(PreviousPage); NextPageCommand = new RelayCommand(NextPage); LastPageCommand = new RelayCommand(LastPage); LoadData(); } private void LoadData() { using (var db = new SqlSugarClient(new ConnectionConfig { ConnectionString = "your connection string" })) { var query = db.Queryable<Person>().OrderBy(p => p.Id); TotalCount = query.Count(); PersonList = query.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(); } } private void FirstPage() { PageIndex = 1; LoadData(); } private void PreviousPage() { if (PageIndex > 1) { PageIndex--; LoadData(); } } private void NextPage() { if (PageIndex < TotalCount / PageSize + 1) { PageIndex++; LoadData(); } } private void LastPage() { PageIndex = TotalCount / PageSize + 1; LoadData(); } } ``` 4. 创建一个名为“Views”的文件夹,并在其中创建一个名为“PersonView.xaml”的视图,用于显示人员数据和分页控件。 ```xml <UserControl x:Class="WpfApp.Views.PersonView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:hc="clr-namespace:HandyControl.Controls;assembly=HandyControl" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:vm="clr-namespace:WpfApp.ViewModels" DataContext="{Binding PersonViewModel, Source={StaticResource Locator}}"> <Grid> <StackPanel> <DataGrid ItemsSource="{Binding PersonList}" SelectedItem="{Binding SelectedPerson}"> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding Id}"/> <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> <DataGridTextColumn Header="Gender" Binding="{Binding Gender}"/> <DataGridTextColumn Header="Date of Birth" Binding="{Binding DateOfBirth}"/> <DataGridTextColumn Header="Address" Binding="{Binding Address}"/> </DataGrid.Columns> </DataGrid> <hc:Pager CurrentPage="{Binding PageIndex}" PageSize="{Binding PageSize}" TotalCount="{Binding TotalCount}"> <i:Interaction.Triggers> <i:EventTrigger EventName="FirstPageClick"> <i:InvokeCommandAction Command="{Binding FirstPageCommand}"/> </i:EventTrigger> <i:EventTrigger EventName="PreviousPageClick"> <i:InvokeCommandAction Command="{Binding PreviousPageCommand}"/> </i:EventTrigger> <i:EventTrigger EventName="NextPageClick"> <i:InvokeCommandAction Command="{Binding NextPageCommand}"/> </i:EventTrigger> <i:EventTrigger EventName="LastPageClick"> <i:InvokeCommandAction Command="{Binding LastPageCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </hc:Pager> </StackPanel> </Grid> </UserControl> ``` 5. 在App.xaml中添加以下代码,以便在应用程序中使用ViewModelLocator。 ```xml <Application.Resources> <ResourceDictionary> <vm:ViewModelLocator x:Key="Locator"/> </ResourceDictionary> </Application.Resources> ``` 6. 在MainWindow.xaml中使用PersonView。 ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:views="clr-namespace:WpfApp.Views" Title="MainWindow" Height="450" Width="800"> <Grid> <views:PersonView/> </Grid> </Window> ``` 现在您已经可以运行应用程序并使用分页控件浏览人员数据了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值