目录
开始前准备的数据库dbblog如下:
第一步:创建项目后下载四个NuGet程序包
第二步:删除原本的MainWindow.XAML文件 并创建如下的目录结构 然后在View文件夹下面创建Login.XAML和Main.XAML 并且在App.XAML中将启动项改为Login.XAML
1.View文件夹下面创建Login.XAML和Main.XAML
2.在App.XAML中将启动项改为Login.XAML
第三步:在Model文件夹中创建BlogTypeInfo类,CommentInfo类,PostInfo类,UserInfo类 并在Model文件夹下的View文件夹中创建BlogTypeInfoView类,CommentInfoView类,PostInfoView类,UserInfoView类
BlogTypeInfo类如下:
CommentInfo类如下:
PostInfo类如下:
UserInfo类如下:
BlogTypeInfoView类如下:
CommentInfoView类如下:
PostInfoView类如下:
UserInfoView类如下:
第四步:在View文件夹中的UserControls文件夹中创建BlogType.XAML、Comment.XAML、Post.XAML、User.XAML
User.XAML如下:
Post.XAML如下:
Comment.XAML如下:
BlogType.XAML如下:
第五步:在Service文件夹下创建IBase 、IBlogTypeInfo、ICommentInfo、IPostInfo、IUserInfo
IBase如下:
IBlogTypeInfo如下:
ICommentInfo如下:
IPostInfo如下:
IUserInfo如下:
第六步:先配置好在APP.config中的connectionStrings并在Help文件夹中创建ConstHelper类中把connectionStrings放在里面 然后实现Service中的所有接口BlogTypeInfoService、CommentInfoService、PostInfoService、UserInfoService
配置connectionStrings
ConstHelper如下:
BlogTypeInfoService如下:
CommentInfoService如下:
PostInfoService如下:
UserInfoService如下:
第七步:在App.cs文件中放入想要放入Ioc容器的对象
第八步:在Helper文件夹中创建LoginInfoHelper类存放登录的用户 并在ViewModel创建LoginViewModel类和MainViewModel类
LoginViewModel如下:
MainViewModel如下:
第九步:在Login.XAML和Main.XAML中写对应的前端代码
1.在Dict文件夹中创建LoginDictionary.xaml存放登录窗口的样式
LoginDictionary.xaml如下:
2.App.XAML文件中存在共同的样式以及存放LoginDictionary.xaml的引用
3.在Login.XAML文件中创建上下文并创建基本样式
4.在Main.XAML文件中创建上下文并创建基本样式
效果如下:
开始前准备的数据库dbblog如下:
USE [dbblog]
GO
/****** Object: Table [dbo].[UserInfo] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserInfo](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](20) NOT NULL,
[UserPwd] [varchar](50) NOT NULL,
[Phone] [varchar](20) NOT NULL,
[NickName] [varchar](20) NOT NULL,
[Logo] [image] NULL,
[RealName] [varchar](20) NULL,
[Sex] [bit] NULL,
[Birthday] [datetime] NULL,
[DataStatus] [int] NOT NULL,
[CreateUserId] [int] NOT NULL,
[CreateTime] [datetime] NOT NULL,
[LastUpdateUserId] [int] NULL,
[LastUpdateTime] [datetime] NULL,
CONSTRAINT [PK_UserInfo] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: View [dbo].[UserInfoView] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[UserInfoView]
as
select ui1.[UserId]
,ui1.[UserName]
,ui1.[UserPwd]
,ui1.[Phone]
,ui1.[NickName]
,ui1.[Logo]
,ui1.[RealName]
,ui1.[Sex]
,ui1.[Birthday]
,ui1.[DataStatus]
,case ui1.[DataStatus] when 0 then '正常' when 1 then '禁用' else '未知' end as DataStatusText
,ui1.[CreateUserId]
,ui2.[UserName] as CreateUserName
,ui1.[CreateTime]
,ui1.[LastUpdateUserId]
,ui3.[UserName] as LastUpdateUserName
,ui1.[LastUpdateTime] from UserInfo as ui1
left join UserInfo as ui2 on ui1.CreateUserId = ui2.UserId
left join UserInfo as ui3 on ui1.LastUpdateUserId = ui2.UserId
GO
/****** Object: Table [dbo].[BlogTypeInfo] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BlogTypeInfo](
[BlogTypeId] [int] IDENTITY(1,1) NOT NULL,
[TypeName] [varchar](50) NOT NULL,
[ParentBlogTypeId] [int] NOT NULL,
[DataStatus] [int] NOT NULL,
[CreateUserId] [int] NOT NULL,
[CreateTime] [datetime] NOT NULL,
[LastUpdateUserId] [int] NULL,
[LastUpdateTime] [datetime] NULL,
CONSTRAINT [PK_BlogTypeInfo] PRIMARY KEY CLUSTERED
(
[BlogTypeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: View [dbo].[BlogTypeInfoView] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[BlogTypeInfoView]
as
select bti.[BlogTypeId]
,bti.[TypeName]
,bti.[ParentBlogTypeId]
,bti2.[TypeName] as ParentBlogTypeName
,bti.[DataStatus]
,case bti.[DataStatus] when 0 then '正常' when 1 then '禁用' else '未知' end as DataStatusText
,bti.[CreateUserId]
,ui1.[UserName] as CreateUserName
,bti.[CreateTime]
,bti.[LastUpdateUserId]
,ui2.[UserName] as LastUpdateUserName
,bti.[LastUpdateTime] from blogtypeinfo as bti
left join blogtypeinfo as bti2 on bti.parentblogtypeid=bti2.blogtypeid
left join userinfo as ui1 on bti.createuserid=ui1.userid
left join userinfo as ui2 on bti.lastupdateuserid=ui2.userid
GO
/****** Object: Table [dbo].[CommentInfo] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CommentInfo](
[CommentId] [int] IDENTITY(1,1) NOT NULL,
[CommentContent] [varchar](250) NOT NULL,
[DataStatus] [int] NOT NULL,
[CreateUserId] [int] NOT NULL,
[CreateTime] [datetime] NOT NULL,
[LastUpdateUserId] [int] NULL,
[LastUpdateTime] [datetime] NULL,
CONSTRAINT [PK_CommentInfo] PRIMARY KEY CLUSTERED
(
[CommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: View [dbo].[CommentInfoView] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[CommentInfoView]
as
SELECT ci.[CommentId]
,ci.[CommentContent]
,ci.[DataStatus]
,case ci.[DataStatus] when 0 then '正常' when 1 then '禁用' else '未知' end as DataStatusText
,ci.[CreateUserId]
,ui1.[UserName] as CreateUserName
,ci.[CreateTime]
,ci.[LastUpdateUserId]
,ui2.[UserName] as LastUpdateUserName
,ci.[LastUpdateTime]
FROM [dbo].[CommentInfo] as ci
left join userinfo as ui1 on ci.createuserid=ui1.userid
left join userinfo as ui2 on ci.lastupdateuserid=ui2.userid
GO
/****** Object: Table [dbo].[PostInfo] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PostInfo](
[PostId] [int] IDENTITY(1,1) NOT NULL,
[Title] [varchar](50) NOT NULL,
[PostContent] [text] NOT NULL,
[BlogTypeId] [int] NOT NULL,
[LikeCount] [int] NOT NULL,
[HateCount] [int] NOT NULL,
[ReadCount] [int] NOT NULL,
[CommentCount] [int] NOT NULL,
[DataStatus] [int] NOT NULL,
[CreateUserId] [int] NOT NULL,
[CreateTime] [datetime] NOT NULL,
[LastUpdateUserId] [int] NULL,
[LastUpdateTime] [datetime] NULL,
CONSTRAINT [PK_PostInfo] PRIMARY KEY CLUSTERED
(
[PostId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: View [dbo].[PostInfoView] Script Date: 2024/11/5 17:42:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[PostInfoView]
as
SELECT poi.[PostId]
,poi.[Title]
,poi.[PostContent]
,poi.[BlogTypeId]
,bti.[TypeName] as BlogTypeName
,poi.[LikeCount]
,poi.[HateCount]
,poi.[ReadCount]
,poi.[CommentCount]
,poi.[DataStatus]
,case poi.[DataStatus] when 0 then '正常' when 1 then '禁用' else '未知' end as DataStatusText
,poi.[CreateUserId]
,ui1.[UserName] as CreateUserName
,poi.[CreateTime]
,poi.[LastUpdateUserId]
,ui2.[UserName] as LastUpdateUserName
,poi.[LastUpdateTime]
FROM [dbo].[PostInfo] as poi
left join blogtypeinfo as bti on poi.blogtypeid=bti.blogtypeId
left join userinfo as ui1 on poi.createuserid=ui1.userid
left join userinfo as ui2 on poi.lastupdateuserid=ui2.userid
GO
ALTER TABLE [dbo].[BlogTypeInfo] ADD CONSTRAINT [DF_BlogTypeInfo_DataStatus] DEFAULT ((0)) FOR [DataStatus]
GO
ALTER TABLE [dbo].[BlogTypeInfo] ADD CONSTRAINT [DF_BlogTypeInfo_CreateTime] DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[CommentInfo] ADD CONSTRAINT [DF_CommentInfo_DataStatus] DEFAULT ((0)) FOR [DataStatus]
GO
ALTER TABLE [dbo].[CommentInfo] ADD CONSTRAINT [DF_CommentInfo_CreateTime] DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_LikeCount] DEFAULT ((0)) FOR [LikeCount]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_HateCount] DEFAULT ((0)) FOR [HateCount]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_ReadCount] DEFAULT ((0)) FOR [ReadCount]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_CommentCount] DEFAULT ((0)) FOR [CommentCount]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_DataStatus] DEFAULT ((0)) FOR [DataStatus]
GO
ALTER TABLE [dbo].[PostInfo] ADD CONSTRAINT [DF_PostInfo_CreateTime] DEFAULT (getdate()) FOR [CreateTime]
GO
ALTER TABLE [dbo].[UserInfo] ADD CONSTRAINT [DF_UserInfo_DataStatus] DEFAULT ((0)) FOR [DataStatus]
GO
ALTER TABLE [dbo].[UserInfo] ADD CONSTRAINT [DF_UserInfo_CreateTime] DEFAULT (getdate()) FOR [CreateTime]
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'BlogTypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'博客类型名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'TypeName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'父级类型名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'ParentBlogTypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据状态(0正常,1禁用)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'DataStatus'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'CreateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'CreateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'BlogTypeInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'CommentId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'评论内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'CommentContent'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据状态(0正常,1禁用)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'DataStatus'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'CreateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'CreateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'CommentInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'PostId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'标题' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'Title'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'PostContent'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'BlogTypeId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'点赞数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'LikeCount'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'踩数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'HateCount'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'阅读数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'ReadCount'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'评论数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'CommentCount'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据状态(0正常,1禁用)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'DataStatus'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'CreateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'CreateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'PostInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'UserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'用户名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'UserName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'密码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'UserPwd'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'手机号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'Phone'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'昵称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'NickName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'姓名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'RealName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'性别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'Sex'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'生日' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'Birthday'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据状态(0正常,1禁用)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'DataStatus'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'CreateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'CreateTime'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改人(外键)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateUserId'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后一次修改日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'UserInfo', @level2type=N'COLUMN',@level2name=N'LastUpdateTime'
GO
第一步:创建项目后下载四个NuGet程序包
第二步:删除原本的MainWindow.XAML文件 并创建如下的目录结构 然后在View文件夹下面创建Login.XAML和Main.XAML 并且在App.XAML中将启动项改为Login.XAML
1.View文件夹下面创建Login.XAML和Main.XAML
2.在App.XAML中将启动项改为Login.XAML
第三步:在Model文件夹中创建BlogTypeInfo类,CommentInfo类,PostInfo类,UserInfo类 并在Model文件夹下的View文件夹中创建BlogTypeInfoView类,CommentInfoView类,PostInfoView类,UserInfoView类
BlogTypeInfo类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model
{
public class BlogTypeInfo
{
public int BlogTypeId { get; set; }
public string TypeName { get; set; }
public int ParentBlogTypeId { get; set; }
public int DataStatus { get; set; } = 0;
public int CreateUserId { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
public int LastUpdateUserId { get; set; }
public DateTime LastUpdateTime { get; set; }
}
}
CommentInfo类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model
{
public class CommentInfo
{
public int CommentId { get; set; }
public string CommentContent { get; set; }
public int DataStatus { get; set; } = 0;
public int CreateUserId { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
public int LastUpdateUserId { get; set; }
public DateTime LastUpdateTime { get; set; }
}
}
PostInfo类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model
{
public class PostInfo
{
public int PostId { get; set; }
public string Title { get; set; }
public string PostContent { get; set; }
public int BlogTypeId { get; set; }
public int LikeCount { get; set; } = 0;
public int HateCount { get; set; } = 0;
public int ReadCount { get; set; } = 0;
public int CommentCount { get; set; } = 0;
public int DataStatus { get; set; } = 0;
public int CreateUserId { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
public int LastUpdateUserId { get; set; }
public DateTime LastUpdateTime { get; set; }
}
}
UserInfo类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model
{
public class UserInfo
{
public int UserId { get; set; }
public string UserName { get; set; }
public string UserPwd { get; set; }
public string Phone { get; set; }
public string NickName { get; set; }
public byte[] Logo { get; set; }
public string RealName { get; set; }
public bool Sex { get; set; }
public DateTime Birthday { get; set; }
public int DataStatus { get; set; } = 0;
public int CreateUserId { get; set; }
public DateTime CreateTime { get; set; } = DateTime.Now;
public int LastUpdateUserId { get; set; }
public DateTime LastUpdateTime { get; set; }
}
}
BlogTypeInfoView类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model.View
{
public class BlogTypeInfoView : BlogTypeInfo
{
public string ParentBlogTypeName { get; set; }
public string DataStatusText { get; set; }
public string CreateUserName { get; set; }
public int LastUpdateUserName { get; set; }
}
}
CommentInfoView类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model.View
{
public class CommentInfoView : CommentInfo
{
public string DataStatusText { get; set; }
public string CreateUserName { get; set; }
public int LastUpdateUserName { get; set; }
}
}
PostInfoView类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model.View
{
public class PostInfoView : PostInfo
{
public string BlogTypeName { get; set; }
public string DataStatusText { get; set; }
public string CreateUserName { get; set; }
public int LastUpdateUserName { get; set; }
}
}
UserInfoView类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Model.View
{
public class UserInfoView : UserInfo
{
public string DataStatusText { get; set; }
public string CreateUserName { get; set; }
public int LastUpdateUserName { get; set; }
}
}
第四步:在View文件夹中的UserControls文件夹中创建BlogType.XAML、Comment.XAML、Post.XAML、User.XAML
User.XAML如下:
<UserControl x:Class="WPF练习13简单的项目.View.UserControls.User"
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:WPF练习13简单的项目.View.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="1" />
</Grid>
</UserControl>
Post.XAML如下:
<UserControl x:Class="WPF练习13简单的项目.View.UserControls.Post"
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:WPF练习13简单的项目.View.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="2" />
</Grid>
</UserControl>
Comment.XAML如下:
<UserControl x:Class="WPF练习13简单的项目.View.UserControls.Comment"
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:WPF练习13简单的项目.View.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="3" />
</Grid>
</UserControl>
BlogType.XAML如下:
<UserControl x:Class="WPF练习13简单的项目.View.UserControls.BlogType"
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:WPF练习13简单的项目.View.UserControls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBlock Text="4" />
</Grid>
</UserControl>
第五步:在Service文件夹下创建IBase 、IBlogTypeInfo、ICommentInfo、IPostInfo、IUserInfo
IBase如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Service
{
public interface IBase<T,K>
{
/// <summary>
/// 添加数据
/// </summary>
/// <param name="model">模型</param>
/// <returns>布尔</returns>
bool Add(T model);
/// <summary>
/// 删除
/// </summary>
/// <param name="id">主键</param>
/// <returns>布尔</returns>
bool Delete(int id);
/// <summary>
/// 分页获取数据
/// </summary>
/// <param name="strWhere">查询条件</param>
/// <param name="orderBy">排序字段</param>
/// <param name="startIndex">起始索引</param>
/// <param name="endIndex">结束索引</param>
/// <returns>列表,关联视图模型K</returns>
List<K> GetListByPage(string strWhere, string orderBy, int startIndex, int endIndex);
/// <summary>
/// 获取单个模型
/// </summary>
/// <param name="id">主键</param>
/// <returns>泛型T</returns>
T GetModel(int id);
/// <summary>
/// 修改
/// </summary>
/// <param name="model">模型</param>
/// <returns>布尔</returns>
bool Update(T model);
}
}
IBlogTypeInfo如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Model;
namespace WPF练习13简单的项目.Service
{
public interface IBlogTypeInfo : IBase<BlogTypeInfo, BlogTypeInfoView>
{
}
}
ICommentInfo如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Model;
namespace WPF练习13简单的项目.Service
{
public interface ICommentInfo : IBase<CommentInfo, CommentInfoView>
{
}
}
IPostInfo如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Model;
namespace WPF练习13简单的项目.Service
{
public interface IPostInfo : IBase<PostInfo, PostInfoView>
{
}
}
IUserInfo如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Model.View;
namespace WPF练习13简单的项目.Service
{
public interface IUserInfo : IBase<UserInfo, UserInfoView>
{
UserInfo Login(string userName, string userPwd);
}
}
第六步:先配置好在APP.config中的connectionStrings并在Help文件夹中创建ConstHelper类中把connectionStrings放在里面 然后实现Service中的所有接口BlogTypeInfoService、CommentInfoService、PostInfoService、UserInfoService
配置connectionStrings
ConstHelper如下:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WPF练习13简单的项目.Helper
{
public static class ConstHelper
{
public static string ConnString { get; } = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
}
}
BlogTypeInfoService如下:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Service;
using Dapper;
using WPF练习13简单的项目.Helper;
namespace WPF练习13简单的项目.ServiceImpl
{
public class BlogTypeInfoService : IBlogTypeInfo
{
public bool Add(BlogTypeInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "INSERT INTO BlogTypeInfo(TypeName,ParentBlogTypeId,DataStatus,CreateUserId,CreateTime,LastUpdateUserId,LastUpdateTime) VALUES (@TypeName,@ParentBlogTypeId,@DataStatus,@CreateUserId,@CreateTime,@LastUpdateUserId,@LastUpdateTime)";
return conn.Execute(sql, model) > 0;
}
}
public bool Delete(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE BlogTypeInfo Set DataStatus=1 WHERE BlogTypeId=@BlogTypeId";
var param = new { BlogTypeId = id };
return conn.Execute(sql, param) > 0;
}
}
public List<BlogTypeInfoView> GetListByPage(string strWhere, string orderBy, int startIndex, int endIndex)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
sql.Append("SELECT * FROM ( ");
sql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderBy.Trim()))
sql.Append("ORDER BY T." + orderBy);
else
sql.Append("ORDER BY T.UserId DESC");
sql.Append(")AS Row, T.* FROM BlogTypeInfoView T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
sql.Append(" WHERE " + strWhere);
}
sql.Append(" ) TT");
sql.AppendFormat(" WHERE TT.Row BETWEEN {0} AND {1}", startIndex, endIndex);
return conn.Query<BlogTypeInfoView>(sql.ToString()).ToList();
}
}
public BlogTypeInfo GetModel(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "SELECT * FROM BlogTypeInfo WHERE BlogTypeId=@BlogTypeId";
var param = new { BlogTypeId = id };
return conn.QueryFirstOrDefault<BlogTypeInfo>(sql, param);
}
}
public bool Update(BlogTypeInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE BlogTypeInfo SET TypeName=@TypeName,ParentBlogTypeId=@ParentBlogTypeId,DataStatus=@DataStatus,CreateUserId=@CreateUserId,CreateTime=@CreateTime,LastUpdateUserId=@LastUpdateUserId,LastUpdateTime=@LastUpdateTime WHERE BlogTypeId=@BlogTypeId";
return conn.Execute(sql, model) > 0;
}
}
}
}
CommentInfoService如下:
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Helper;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Service;
namespace WPF练习13简单的项目.ServiceImpl
{
public class CommentInfoService : ICommentInfo
{
public bool Add(CommentInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "INSERT INTO CommentInfo(CommentContent,DataStatus,CreateUserId,CreateTime,LastUpdateUserId,LastUpdateTime) VALUES (@CommentContent,@DataStatus,@CreateUserId,@CreateTime,@LastUpdateUserId,@LastUpdateTime)";
return conn.Execute(sql, model) > 0;
}
}
public bool Delete(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE CommentInfo Set DataStatus=1 WHERE CommentId=@CommentId";
var param = new { CommentId = id };
return conn.Execute(sql, param) > 0;
}
}
public List<CommentInfoView> GetListByPage(string strWhere, string orderBy, int startIndex, int endIndex)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
sql.Append("SELECT * FROM ( ");
sql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderBy.Trim()))
sql.Append("ORDER BY T." + orderBy);
else
sql.Append("ORDER BY T.UserId DESC");
sql.Append(")AS Row, T.* FROM CommentInfoView T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
sql.Append(" WHERE " + strWhere);
}
sql.Append(" ) TT");
sql.AppendFormat(" WHERE TT.Row BETWEEN {0} AND {1}", startIndex, endIndex);
return conn.Query<CommentInfoView>(sql.ToString()).ToList();
}
}
public CommentInfo GetModel(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "SELECT * FROM CommentInfo WHERE CommentId=@CommentId";
var param = new { CommentId = id };
return conn.QueryFirstOrDefault<CommentInfo>(sql, param);
}
}
public bool Update(CommentInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE CommentInfo SET CommentContent=@CommentContent,DataStatus=@DataStatus,CreateUserId=@CreateUserId,CreateTime=@CreateTime,LastUpdateUserId=@LastUpdateUserId,LastUpdateTime=@LastUpdateTime WHERE CommentId=@CommentId";
return conn.Execute(sql, model) > 0;
}
}
}
}
PostInfoService如下:
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Helper;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Service;
namespace WPF练习13简单的项目.ServiceImpl
{
public class PostInfoService: IPostInfo
{
public bool Add(PostInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "INSERT INTO PostInfo(Title,PostContent,BlogTypeId,LikeCount,HateCount,ReadCount,CommentCount,DataStatus,CreateUserId,CreateTime,LastUpdateUserId,LastUpdateTime) VALUES (@Title,@PostContent,@BlogTypeId,@LikeCount,@HateCount,@ReadCount,@CommentCount,@DataStatus,@CreateUserId,@CreateTime,@LastUpdateUserId,@LastUpdateTime)";
return conn.Execute(sql, model) > 0;
}
}
public bool Delete(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE PostInfo Set DataStatus=1 WHERE PostId=@PostId";
var param = new { PostId = id };
return conn.Execute(sql, param) > 0;
}
}
public List<PostInfoView> GetListByPage(string strWhere, string orderBy, int startIndex, int endIndex)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
sql.Append("SELECT * FROM ( ");
sql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderBy.Trim()))
sql.Append("ORDER BY T." + orderBy);
else
sql.Append("ORDER BY T.UserId DESC");
sql.Append(")AS Row, T.* FROM PostInfoView T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
sql.Append(" WHERE " + strWhere);
}
sql.Append(" ) TT");
sql.AppendFormat(" WHERE TT.Row BETWEEN {0} AND {1}", startIndex, endIndex);
return conn.Query<PostInfoView>(sql.ToString()).ToList();
}
}
public PostInfo GetModel(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "SELECT * FROM PostInfo WHERE PostId=@PostId";
var param = new { PostId = id };
return conn.QueryFirstOrDefault<PostInfo>(sql, param);
}
}
public bool Update(PostInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE PostInfo SET Title=@Title,PostContent=@PostContent,BlogTypeId=@BlogTypeId,LikeCount=@LikeCount,HateCount=@HateCount,ReadCount=@ReadCount,CommentCount=@CommentCount,DataStatus=@DataStatus,CreateUserId=@CreateUserId,CreateTime=@CreateTime,LastUpdateUserId=@LastUpdateUserId,LastUpdateTime=@LastUpdateTime WHERE PostId=@PostId";
return conn.Execute(sql, model) > 0;
}
}
}
}
UserInfoService如下:
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WPF练习13简单的项目.Helper;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Model.View;
using WPF练习13简单的项目.Service;
namespace WPF练习13简单的项目.ServiceImpl
{
public class UserInfoService: IUserInfo
{
public bool Add(UserInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "INSERT INTO UserInfo(UserName,UserPwd,Phone,NickName,Logo,RealName,Sex,Birthday,DataStatus,CreateUserId,CreateTime,LastUpdateUserId,LastUpdateTime) VALUES (@UserName,@UserPwd,@Phone,@NickName,@Logo,@RealName,@Sex,@Birthday,@DataStatus,@CreateUserId,@CreateTime,@LastUpdateUserId,@LastUpdateTime)";
return conn.Execute(sql, model) > 0;
}
}
public bool Delete(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE UserInfo Set DataStatus=1 WHERE UserId=@UserId";
var param = new { UserId = id };
return conn.Execute(sql, param) > 0;
}
}
public List<UserInfoView> GetListByPage(string strWhere, string orderBy, int startIndex, int endIndex)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
StringBuilder sql = new StringBuilder();
sql.Append("SELECT * FROM ( ");
sql.Append(" SELECT ROW_NUMBER() OVER (");
if (!string.IsNullOrEmpty(orderBy.Trim()))
sql.Append("ORDER BY T." + orderBy);
else
sql.Append("ORDER BY T.UserId DESC");
sql.Append(")AS Row, T.* FROM UserInfoView T ");
if (!string.IsNullOrEmpty(strWhere.Trim()))
{
sql.Append(" WHERE " + strWhere);
}
sql.Append(" ) TT");
sql.AppendFormat(" WHERE TT.Row BETWEEN {0} AND {1}", startIndex, endIndex);
return conn.Query<UserInfoView>(sql.ToString()).ToList();
}
}
public UserInfo GetModel(int id)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "SELECT * FROM UserInfo WHERE UserId=@UserId";
var param = new { UserId = id };
return conn.QueryFirstOrDefault<UserInfo>(sql, param);
}
}
public UserInfo Login(string userName, string userPwd)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "SELECT * FROM UserInfo WHERE UserName=@UserName AND UserPwd=@UserPwd";
var param = new { UserName = userName, UserPwd = userPwd };
return conn.QueryFirstOrDefault<UserInfo>(sql, param);
}
}
public bool Update(UserInfo model)
{
using (var conn = new SqlConnection(ConstHelper.ConnString))
{
conn.Open();
var sql = "UPDATE UserInfo SET UserName=@UserName,UserPwd=@UserPwd,Phone=@Phone,NickName=@NickName,Logo=@Logo,RealName=@RealName,Sex=@Sex,Birthday=@Birthday,DataStatus=@DataStatus,CreateUserId=@CreateUserId,CreateTime=@CreateTime,LastUpdateUserId=@LastUpdateUserId,LastUpdateTime=@LastUpdateTime WHERE UserId=@UserId";
return conn.Execute(sql, model) > 0;
}
}
}
}
第七步:在App.cs文件中放入想要放入Ioc容器的对象
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WPF练习13简单的项目.Service;
using WPF练习13简单的项目.ServiceImpl;
using WPF练习13简单的项目.View;
using WPF练习13简单的项目.View.UserControls;
namespace WPF练习13简单的项目
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public IServiceProvider Services { get; }
public App()
{
Services = ConfigurationServices();
}
public static new App Current { get { return (App)Application.Current; } }
private IServiceProvider ConfigurationServices()
{
ServiceCollection services = new ServiceCollection();
// 放窗体
services.AddSingleton<Main>();
// 放用户控件
services.AddSingleton<BlogType>();
services.AddSingleton<Comment>();
services.AddSingleton<Post>();
services.AddSingleton<User>();
// 放服务
services.AddSingleton<IBlogTypeInfo, BlogTypeInfoService>();
services.AddSingleton<ICommentInfo, CommentInfoService>();
services.AddSingleton<IPostInfo, PostInfoService>();
services.AddSingleton<IUserInfo, UserInfoService>();
return services.BuildServiceProvider();
}
}
}
第八步:在Helper文件夹中创建LoginInfoHelper类存放登录的用户 并在ViewModel创建LoginViewModel类和MainViewModel类
using CTMVVMDemo.Model;
namespace CTMVVMDemo.Helper
{
public static class LoginInfoHelper
{
public static UserInfo CurrentUser { get; set; }
}
}
LoginViewModel如下:
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using WPF练习13简单的项目.Helper;
using WPF练习13简单的项目.Model;
using WPF练习13简单的项目.Service;
using WPF练习13简单的项目.View;
namespace WPF练习13简单的项目.ViewModel
{
public class LoginViewModel : ObservableObject
{
private readonly IUserInfo userService;
public LoginViewModel()
{
userService = App.Current.Services.GetService<IUserInfo>();
}
private string userName;
public string UserName
{
get => userName;
set => SetProperty(ref userName, value);
}
private SecureString password;
public SecureString Password
{
get => password;
set => SetProperty(ref password, value);
}
public RelayCommand<Login> LoginCommand
{
get
{
return new RelayCommand<Login>((loginWin) =>
{
Button btnLogin = loginWin.FindName("btnLogin") as Button;
if (btnLogin != null) btnLogin.Focus();
UserInfo userInfo = userService.Login(UserName, SecureStringToString(Password));
if (userInfo == null)
{
MessageBox.Show("登录失败,请检查账号或密码是否正确!", "错误", System.Windows.MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
LoginInfoHelper.CurrentUser = userInfo;
loginWin.Hide();
var mainWin = App.Current.Services.GetService<Main>();
mainWin.Show();
});
}
}
public RelayCommand<object> PasswordChangedCommand
{
get
{
return new RelayCommand<object>((obj) =>
{
var passwo