优质WPF免费学习资源分享(含代码)

WPF自学资源分享

背景

自身是winform开发,winform岗位比较少。wpf和winfom殊途同归,所以自身最近也在学习wpf。

分享一下自己寻找到的wpf学习资源,希望对大家有帮助。

学习资源

推荐书籍学习资源

《深入简出WPF》-刘铁猛

作者是微软的高级开发工程师,多年的wpf开发经验。

不过作者多NB不重要,主要是这本书容易懂。

学习门槛低,更容易快速上手。经验者初学者都合适。

《WPF高级编程》

这本书相对更细节更全面的学习WPF,适合多年经验者精进学习。

推荐博客学习资源

微软官方文档

WPF 介绍 | Microsoft Learn

上面是平时些东西的时候去查询的。

先行者的博客

WPF入门教程系列一——基础 - DotNet菜园 - 博客园 (cnblogs.com)

👆上面的地址是我快速学习上手的博客,快速讲解常用控件,依赖属性,绑定,命令等等。

基本上,上面的博客你看完了,可以直接上手写东西了。

推荐视频学习资源

刘铁猛-深入浅出WPF-剪辑版-全5课-分19集_哔哩哔哩_bilibili

👆上面的课程是我接下来准备去学习的。

学习路径

有基本开发经验,推荐下面学习路线

>  1.快速扫盲基本知识   (先行者博客)

>  2.直接动手写Demo  (直接实现一个基本的简单应用,git地址放在文章最后)

>  3.进一步深入学习 (深入全面学习,为了面试#视频学习资源)

经验者学习路线:1 > 2 > 3

初学者学习路线:3 > 1 > 2

代码demo

贪吃蛇小游戏

WPF教程之 改善WPF贪吃蛇:加一个高分榜_Vue5教程

ToDoList

自己写的复杂Demo。

包含常用控件TextBox、GridView、ListView、折线图等等常用控件;

使用的WPF Prism、Mvvm开发框架;

涉及到依赖对象和数据绑定、路由事件与命令、数据模板与空间模板等等;

数据库方面直接用的Ado.Net,这个不是重点。

👆创表语句在这里

using LiveCharts;
using LiveCharts.Wpf;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using WPFToDoList.Data;
using WPFToDoList.Model;
using WPFToDoList.Service;
using WPFToDoList.View;


namespace WPFToDoList.ViewModel
{
    /// <summary>
    /// ViewModel层
    /// </summary>
    public class TaskViewModel : BindableBase, INavigationAware
    {
        #region 初始化
        public TaskViewModel()
        {
            CurrentUserName = "帅气的卡夫卡";
            InitializeTimer();
            TaskList = GetTaskData();
            CurrentPageNumber = 1;
            Items = new ObservableCollection<TaskModel>(GetPageItems(TaskList, _currentPageNumber, PageSize));
            SaveCommand = new RelayCommand(SaveCommand_Execute);
            SelectTask = new TaskModel();
        }

        public TaskService taskService = new TaskService();
        /// <summary>
        /// 查询任务数据
        /// </summary>
        /// <returns></returns>
        public List<TaskModel> GetTaskData()
        {
            var res = taskService.GetTaskData(Data.OptionType.onLineType);
            TotalCount = res.Count;
            tmpTaskList = res;
            InitializeChart();
            return res;
        }
        #endregion

        #region 定时器显示时间日期
        private DispatcherTimer _timer;

        private void InitializeTimer()
        {
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Tick += Timer_Tick;
            _timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            DayOfWeek = DateTime.Now.DayOfWeek;
            CurrentTime = DateTime.Now;
        }
        #endregion

        #region 分页逻辑
        private int _pageSize = 15;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (value < 1 || value > 20)

                    SetProperty(ref _pageSize, 15);
                else
                    SetProperty(ref _pageSize, value);
            }
        }

        public ObservableCollection<TaskModel> Items { get; set; } // 绑定到ListView的数据源

        // 总项数,用于计算总页数
        public int TotalCount { get; set; }

        private RelayCommand pageChangedCommand;
        /// <summary>
        /// 保存
        /// </summary>
        public RelayCommand PageChangedCommand
        {
            get
            {
                if (pageChangedCommand == null)
                    pageChangedCommand = new RelayCommand(PageChangedCommand_Execute);
                return pageChangedCommand;
            }
            set { pageChangedCommand = value; }
        }

        public void PageChangedCommand_Execute(object parameter)
        {
            int pageNo = Convert.ToInt32(parameter);
            PageChanged(pageNo);
            //CurrentPageNumber = pageNo;
        }

        // 翻页方法
        private void PageChanged(int pageNumber)
        {
            if (pageNumber > 0 && pageNumber <= Math.Max(TotalCount / PageSize, 1))
            {
                Items.Clear();
                var pageItems = GetPageItems(TaskList, pageNumber, PageSize);
                foreach (var item in pageItems)
                {
                    Items.Add(item);
                }
                CurrentPageNumber = pageNumber;
            }
        }

        // 根据当前页码和每页显示的项数获取数据
        private IEnumerable<TaskModel> GetPageItems(List<TaskModel> allItems, int pageNumber, int pageSize)
        {
            return allItems.Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        private int _currentPageNumber;
        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPageNumber
        {
            get { return _currentPageNumber; }
            set
            {
                SetProperty(ref _currentPageNumber, value);
            }
        }

        public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
        #endregion

        #region 常规命令
        private RelayCommand saveCommand;
        /// <summary>
        /// 保存
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                    saveCommand = new RelayCommand(SaveCommand_Execute);
                return saveCommand;
            }
            set { saveCommand = value; }
        }

        public void SaveCommand_Execute(object parameter)
        {
            try
            {
                if (SelectTask != null)
                {
                    int res = taskService.UpdateTaskData(OptionType.onLineType, SelectTask);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(@"更新失败,提示信息:{0}", ex.Message));
            }
            finally
            {
                QueryCommand_Execute(null);
            }
        }

        private RelayCommand deleteCommand;
        /// <summary>
        /// 删除
        /// </summary>
        public RelayCommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                    deleteCommand = new RelayCommand(DeleteCommand_Execute);
                return deleteCommand;
            }
            set { deleteCommand = value; }
        }

        public void DeleteCommand_Execute(object parameter)
        {
            try
            {
                if (SelectTask != null)
                {
                    int res = taskService.DeleteTaskData(OptionType.onLineType, SelectTask);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(@"删除失败,提示信息:{0}", ex.Message));
            }
            finally
            {
                TaskList.Remove(SelectTask);
                tmpTaskList.Remove(SelectTask);
                InitializeChart();
            }
        }

        private RelayCommand addCommand;
        /// <summary>
        /// 新增
        /// </summary>
        public RelayCommand AddCommand
        {
            get
            {
                if (addCommand == null)
                    addCommand = new RelayCommand(AddCommand_Execute);
                return addCommand;
            }
            set { addCommand = value; }
        }

        public void AddCommand_Execute(object parameter)
        {
            AddTaskView addTaskView = new AddTaskView(this);
            addTaskView.Show();
        }

        private RelayCommand queryCommand;
        /// <summary>
        /// 查询
        /// </summary>
        public RelayCommand QueryCommand
        {
            get
            {
                if (queryCommand == null)
                    queryCommand = new RelayCommand(QueryCommand_Execute);
                return queryCommand;
            }
            set { queryCommand = value; }
        }

        private void QueryCommand_Execute(object obj)
        {
            TaskList = GetTaskData();
            TotalCount = TaskList.Count;
            PageChangedCommand_Execute(CurrentPageNumber);
        }

        private RelayCommand queryCommandUndo;
        /// <summary>
        /// 查询未开始
        /// </summary>
        public RelayCommand QueryCommandUndo
        {
            get
            {
                if (queryCommandUndo == null)
                    queryCommandUndo = new RelayCommand(QueryCommandUndo_Execute);
                return queryCommandUndo;
            }
            set { queryCommandUndo = value; }
        }

        private void QueryCommandUndo_Execute(object obj)
        {
            List<TaskModel> filterTasks = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("0")).ToList();
            TaskList = filterTasks;
            TotalCount = filterTasks.Count;
            PageChangedCommand_Execute(CurrentPageNumber);
        }

        private RelayCommand queryCommandDone;
        /// <summary>
        /// 查询已完成
        /// </summary>
        public RelayCommand QueryCommandDone
        {
            get
            {
                if (queryCommandDone == null)
                    queryCommandDone = new RelayCommand(QueryCommandDone_Execute);
                return queryCommandDone;
            }
            set { queryCommandDone = value; }
        }

        private void QueryCommandDone_Execute(object obj)
        {
            List<TaskModel> filterTasks = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("2")).ToList();
            TaskList = filterTasks;
            TotalCount = filterTasks.Count;
            PageChangedCommand_Execute(CurrentPageNumber);
        }

        private RelayCommand queryCommandDoing;
        /// <summary>
        /// 查询未进行中
        /// </summary>
        public RelayCommand QueryCommandDoing
        {
            get
            {
                if (queryCommandDoing == null)
                    queryCommandDoing = new RelayCommand(QueryCommandDoing_Execute);
                return queryCommandDoing;
            }
            set { queryCommandDoing = value; }
        }

        private void QueryCommandDoing_Execute(object obj)
        {
            List<TaskModel> filterTasks = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("1")).ToList();
            TaskList = filterTasks;
            TotalCount = filterTasks.Count;
            PageChangedCommand_Execute(CurrentPageNumber);
        }

        #endregion

        #region 常规变量
        private TaskModel selectTask;
        /// <summary>
        /// 选中任务行
        /// </summary>
        public TaskModel SelectTask
        {
            get { return selectTask; }
            set
            {
                selectTask = value; RaisePropertyChanged(nameof(SelectTask));
            }
        }

        /// <summary>
        /// 当前时间
        /// </summary>
        private DateTime currentTime;
        public DateTime CurrentTime
        {
            get { return currentTime; }
            set { SetProperty(ref currentTime, value); }
        }

        /// <summary>
        /// 星期几
        /// </summary>
        private DayOfWeek dayOfWeek;
        public DayOfWeek DayOfWeek
        {
            get { return dayOfWeek; }
            set { SetProperty(ref dayOfWeek, value); }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        private string currentUserName;
        public string CurrentUserName
        {
            get { return currentUserName; }
            set { SetProperty(ref currentUserName, value); }
        }

        /// <summary>
        /// 任务数据
        /// </summary>
        private List<TaskModel> taskList;
        public List<TaskModel> TaskList
        {
            get { return taskList; }
            set { SetProperty(ref taskList, value); }
        }
        /// <summary>
        /// 缓存数据
        /// </summary>
        public List<TaskModel> tmpTaskList;
        #endregion

        #region 统计图表

        private void InitializeChart()
        {
            SeriesCollection = new SeriesCollection
            {
                 new ColumnSeries
                 {
                     Title = "数量",
                     Values = new ChartValues<double>(GetChartArr())
                 }
            };
            XAxisLabels.Clear();
            XAxisLabels.Add("未开始");
            XAxisLabels.Add("进行中");
            XAxisLabels.Add("已完成");
        }
        public ObservableCollection<string> XAxisLabels { get; set; } = new ObservableCollection<string>();

        private double[] GetChartArr()
        {
            int countUndo = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("0")).Count();
            int countDoing = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("1")).Count();
            int countDone = tmpTaskList.AsEnumerable().Where(o => o.TaskStatus.Equals("2")).Count();
            ShowTotalMessage = string.Format("全部任务:{0}个", tmpTaskList.Count);
            ShowTotalMessage0 = string.Format("未完成任务:{0}个", countUndo);
            ShowTotalMessage1 = string.Format("进行中任务:{0}个", countDoing);
            ShowTotalMessage2 = string.Format("已完成任务:{0}个", countDone);
            return new double[] { countUndo, countDoing, countDone };
        }

        private SeriesCollection seriesCollection;
        public LiveCharts.SeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set
            {
                SetProperty(ref seriesCollection, value);
            }
        }

        public Axis XAxis { get; set; }
        public Axis YAxis { get; set; }

        private string showTotalMessage;
        public string ShowTotalMessage
        {
            get { return showTotalMessage; }
            set { SetProperty(ref showTotalMessage, value); }
        }

        private string showTotalMessage0;
        public string ShowTotalMessage0
        {
            get { return showTotalMessage0; }
            set { SetProperty(ref showTotalMessage0, value); }
        }

        private string showTotalMessage1;
        public string ShowTotalMessage1
        {
            get { return showTotalMessage1; }
            set { SetProperty(ref showTotalMessage1, value); }
        }

        private string showTotalMessage2;
        public string ShowTotalMessage2
        {
            get { return showTotalMessage2; }
            set { SetProperty(ref showTotalMessage2, value); }
        }
        #endregion

        #region 页面导航
        //需要继承INavigationAware接口,实现接口中三个方法
        //三个方法的执行顺序:
        //1.Main====>PageA
        //先执行Main页面的OnNavigatedFrom,再执行PageA页面的OnNavigatedTo
        //2.PageA====>PageA
        //OnNavigatedFrom()----->IsNavigationTarget()----->OnNavigatedTo()
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            //每次重新导航时,是否重新创建当前PageBViewModel实例,
            //return true;时就不会再创建新的实例(不会再次运行当前PageBViewModel构造函数)
            return true;
        }

        /// <summary>
        /// // 从这个ViewModel导航离开时的逻辑
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// // 导航到此ViewModel时的逻辑
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            throw new NotImplementedException();
        }
        #endregion

    }
}

Git地址

Talk is cheap show me the code

GitHub地址:https://github.com/wanghuayu-hub2021/WPFToList

👆👆👆👆👆

免费下载代码,希望各位coder能帮忙点亮GIt和Csdn的收藏。

希望各位coder都能找到自己满意的工作,早日财务自由。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值