WPF实战学习笔记19-备忘录添加功能

这篇具有很好参考价值的文章主要介绍了WPF实战学习笔记19-备忘录添加功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

备忘录添加功能

由于todoview 和 memoview的相似度很高,可复制todoview 的代码。文章来源地址https://www.toymoban.com/news/detail-614376.html

memoviewmodel.cs

using Mytodo.Common.Models;
using Mytodo.Service;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using MyToDo.Share.Models;
using System.Threading.Tasks;
using Prism.Regions;
using System.Windows;

namespace Mytodo.ViewModels
{
    public class MemoViewModel : NavigationViewModel
    {
        #region 命令定义

            /// <summary>
            /// 展开侧边栏
            /// </summary>
            public DelegateCommand OpenRightContentCmd { set; get; }

        /// <summary>
        /// 打开选择的项
        /// </summary>
        public DelegateCommand<MemoDto> SelectedCommand { get; set; }

        /// <summary>
        /// 添加、编辑 项
        /// </summary>
        public DelegateCommand<string> ExecuteCommand { get; set; }

        /// <summary>
        /// 删除项
        /// </summary>
        public DelegateCommand<MemoDto> DeleteCommand { get; set; }

        #endregion

            #region 属性定义


            /// <summary>
            /// 项目状态
            /// </summary>
            public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 当前选中项
        /// </summary>
        public MemoDto? CurrDto
        {
            get { return currDto; }
            set { currDto = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 指示侧边栏是否展开
        /// </summary>
        public bool IsRightOpen
        {
            get { return isRightOpen; }
            set { isRightOpen = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// todo集合
        /// </summary>
        public ObservableCollection<MemoDto>? MemoDtos
        {
            get { return memoDtos; }
            set { memoDtos = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 右侧侧边栏标题
        /// </summary>
        public string RightContentTitle
        {
            get { return rightContentTitle; }
            set { rightContentTitle = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 要搜索的字符串
        /// </summary>
        public string SearchString
        {
            get { return search; }
            set { search = value; RaisePropertyChanged(); }
        }

        #endregion

            #region 重要字段定义

            private readonly IMemoService service;

        #endregion

            #region 字段定义
            private int selectIndex;
        private MemoDto currDto;
        private bool isRightOpen;
        private ObservableCollection<MemoDto>? memoDtos;
        private string rightContentTitle;
        private string search;



        #endregion

            #region 命令方法


            /// <summary>
            /// 删除指定项
            /// </summary>
            /// <param name="dto"></param>
            async private void DeleteItem(MemoDto dto)
        {
            var delres = await service.DeleteAsync(dto.Id);

            if (delres.Status)
            {
                var model = MemoDtos.FirstOrDefault(t => t.Id.Equals(dto.Id));
                MemoDtos.Remove(dto);
            }
        }


        private void ExceuteCmd(string obj)
        {
            switch (obj)
            {
                case "添加":
                    Add(); break;
                case "查询":
                    Query(); break;
                case "保存":
                    Save(); break;
            }
        }


        /// <summary>
        /// 保存消息
        /// </summary>
        private async void Save()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(CurrDto.Title) || string.IsNullOrWhiteSpace(CurrDto.Content))
                    return;

                UpdateLoding(true);

                if (CurrDto.Id > 0) //编辑项
                {
                    var updateres = await service.UpdateAsync(CurrDto);
                    if (updateres.Status)
                    {
                        UpdateDataAsync();
                    }
                    else
                    {
                        MessageBox.Show("更新失败");

                    }

                }
                else
                {
                    //添加项
                    var add_res = await service.AddAsync(CurrDto);
                    //刷新
                    if (add_res.Status) //如果添加成功
                    {
                        MemoDtos.Add(add_res.Result);
                    }
                    else
                    {
                        MessageBox.Show("添加失败");

                    }

                }
            }
            catch
            {

            }
            finally
            {
                IsRightOpen = false;
                //卸载数据加载窗体
                UpdateLoding(false);
            }
        }


        /// <summary>
        /// 打开待办事项弹窗
        /// </summary>
        void Add()
        {
            CurrDto = new MemoDto();
            IsRightOpen = true;
        }

        private void Query()
        {
            GetDataAsync();
        }



        /// <summary>
        /// 根据条件更新数据
        /// </summary>
        async void UpdateDataAsync()
        {

            var memoResult = await service.GetAllAsync(new MyToDo.Share.Parameters.QueryParameter { PageIndex = 0, PageSize = 100, Search = SearchString });

            if (memoResult.Status)
            {
                MemoDtos.Clear();
                foreach (var item in memoResult.Result.Items)
                    MemoDtos.Add(item);
            }
        }



        /// <summary>
        /// 获取所有数据
        /// </summary>
        async void GetDataAsync()
        {
            //调用数据加载页面
            UpdateLoding(true);

            //更新数据
            UpdateDataAsync();

            //卸载数据加载页面
            UpdateLoding(false);
        }


        /// <summary>
        /// 弹出详细信息
        /// </summary>
        /// <param name="obj"></param>
        private async void Selected(MemoDto obj)
        {
            var todores = await service.GetFirstOfDefaultAsync(obj.Id);
            if (todores.Status)
            {
                CurrDto = todores.Result;
                IsRightOpen = true;
                RightContentTitle = "我的待办";
            }
        }

        #endregion

            public MemoViewModel(IMemoService service, IContainerProvider provider) : base(provider)
        {
            //初始化对象
            MemoDtos = new ObservableCollection<MemoDto>();
            RightContentTitle = "添加备忘率";

            //初始化命令
            SelectedCommand = new DelegateCommand<MemoDto>(Selected);
            OpenRightContentCmd = new DelegateCommand(Add);
            ExecuteCommand = new DelegateCommand<string>(ExceuteCmd);
            DeleteCommand = new DelegateCommand<MemoDto>(DeleteItem);

            this.service = service;
        }



        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);
            GetDataAsync();
        }

    }
}

memo.view

<UserControl
    x:Class="Mytodo.Views.MemoView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cv="clr-namespace:Mytodo.Common.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:local="clr-namespace:Mytodo.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary>
            <cv:IntToVisibilityConveter x:Key="IntToVisility" />
        </ResourceDictionary>
    </UserControl.Resources>
    <md:DialogHost>
        <md:DrawerHost IsRightDrawerOpen="{Binding IsRightOpen}">
            <md:DrawerHost.RightDrawerContent>
                <DockPanel
                    MinWidth="200"
                    MaxWidth="240"
                    Margin="2"
                    LastChildFill="False">
                    <TextBlock
                        Margin="10"
                        DockPanel.Dock="Top"
                        FontFamily="微软雅黑"
                        FontSize="20"
                        FontWeight="Bold"
                        Text="{Binding RightContentTitle}" />

                    <StackPanel
                        Margin="10"
                        DockPanel.Dock="Top"
                        Orientation="Horizontal">
                        <TextBlock
                            Margin="5"
                            VerticalAlignment="Center"
                            FontFamily="微软雅黑"
                            FontSize="14"
                            Text="状态" />
                    </StackPanel>
                    <TextBox
                        Margin="10"
                        md:HintAssist.Hint="备忘录事项标题"
                        DockPanel.Dock="Top"
                        FontFamily="微软雅黑"
                        FontSize="12"
                        Text="{Binding CurrDto.Title}" />
                    <TextBox
                        MinHeight="50"
                        Margin="10"
                        md:HintAssist.Hint="备忘录事项内容"
                        DockPanel.Dock="Top"
                        FontFamily="微软雅黑"
                        FontSize="12"
                        Text="{Binding CurrDto.Content}"
                        TextWrapping="Wrap" />
                    <Button
                        Margin="10,5"
                        HorizontalAlignment="Center"
                        Command="{Binding ExecuteCommand}"
                        CommandParameter="保存"
                        Content="保存"
                        DockPanel.Dock="Top" />
                </DockPanel>
            </md:DrawerHost.RightDrawerContent>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <StackPanel Margin="15,10" Orientation="Horizontal">
                    <TextBox
                        Width="200"
                        md:HintAssist.Hint="查找备忘录中"
                        md:TextFieldAssist.HasClearButton="True"
                        FontFamily="微软雅黑"
                        FontSize="14"
                        Text="{Binding SearchString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                        <TextBox.InputBindings>
                            <KeyBinding
                                Key="Enter"
                                Command="{Binding ExecuteCommand}"
                                CommandParameter="查询" />
                        </TextBox.InputBindings>
                    </TextBox>

                </StackPanel>
                <Button
                    Margin="10,0"
                    HorizontalAlignment="Right"
                    Command="{Binding OpenRightContentCmd}"
                    Content="+ 添加到备忘"
                    FontFamily="微软雅黑"
                    FontSize="14" />
                <StackPanel
                    Grid.Row="1"
                    VerticalAlignment="Center"
                    Visibility="{Binding MemoDtos.Count, Converter={StaticResource IntToVisility}}">
                    <md:PackIcon
                        Width="120"
                        Height="120"
                        HorizontalAlignment="Center"
                        Kind="ClipboardText" />
                    <TextBlock
                        Margin="0,10"
                        HorizontalAlignment="Center"
                        FontSize="18"
                        Text="尝试添加一些待办事项,以便在此处查看它们。" />
                </StackPanel>
                <ItemsControl
                    Grid.Row="1"
                    Margin="10"
                    ItemsSource="{Binding MemoDtos}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border MinWidth="200" Margin="10">
                                <Grid MinHeight="150">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="MouseLeftButtonUp">
                                            <i:InvokeCommandAction Command="{Binding DataContext.SelectedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" CommandParameter="{Binding}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto" />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <DockPanel Panel.ZIndex="2" LastChildFill="False">
                                        <TextBlock
                                            Margin="10,10"
                                            FontFamily="黑体"
                                            FontSize="14"
                                            Text="{Binding Title}" />
                                        <md:PopupBox
                                            Margin="5"
                                            Panel.ZIndex="1"
                                            DockPanel.Dock="Right">
                                            <Button
                                                Panel.ZIndex="2"
                                                Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}"
                                                CommandParameter="{Binding}"
                                                Content="删除" />
                                        </md:PopupBox>
                                    </DockPanel>
                                    <TextBlock
                                        Grid.Row="1"
                                        Margin="10,5"
                                        FontFamily="黑体"
                                        FontSize="12"
                                        Opacity="0.7"
                                        Text="{Binding Content}" />
                                    <Canvas Grid.RowSpan="2" ClipToBounds="True">
                                        <Border
                                            Canvas.Top="10"
                                            Canvas.Right="-50"
                                            Width="120"
                                            Height="120"
                                            Background="#FFFFFF"
                                            CornerRadius="100"
                                            Opacity="0.1" />
                                        <Border
                                            Canvas.Top="80"
                                            Canvas.Right="-30"
                                            Width="120"
                                            Height="120"
                                            Background="#FFFFFF"
                                            CornerRadius="100"
                                            Opacity="0.1" />
                                    </Canvas>
                                    <Border
                                        Grid.RowSpan="2"
                                        Background="#ffffcc"
                                        CornerRadius="5"
                                        Opacity="0.3" />
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </md:DrawerHost>
    </md:DialogHost>
</UserControl>

修改控制器

public async Task<ApiReponse> Delete(int todoid) => await service.DeleteApublic async Task<ApiReponse> Delete(int todoid) =>

到了这里,关于WPF实战学习笔记19-备忘录添加功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android安卓备忘录(笔记)大作业简单实现有源码注释详细

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 Notedatabase,java MainAcitivity.java

    2024年02月07日
    浏览(39)
  • 【Go语言实战】(22) gin+micro v4+rabbitmq+etcd 重构备忘录

    最近稍微重构了之前写的 micro-todolist 模块 项目地址:https://github.com/CocaineCong/micro-todoList 本次升级将原有的micro v2升级到了micro v4版本,v5 still deving,所以可能不太稳定,所以选择了v4版本。 micro相对于grpc,区别就是 grpc比较原始 ,什么都要自己封装,比如 服务的注册与发现

    2024年02月07日
    浏览(26)
  • 【Golang项目实战】手把手教你写一个备忘录程序|附源码——建议收藏

    博主简介: 努力学习的大一在校计算机专业学生,热爱学习和创作。目前在学习和分享:数据结构、Go,Java等相关知识。 博主主页: @是瑶瑶子啦 所属专栏: Go语言核心编程 近期目标: 写好专栏的每一篇文章 前几天瑶瑶子学习了Go语言的基础语法知识,那么今天我们就写个

    2024年02月06日
    浏览(42)
  • 手机备忘录怎么设置提醒 备忘录提醒设置方法

    具备提醒功能的手机备忘录有很多,比如云便签的提醒功能十分好用,不仅可设置单次定时提醒,还能添加重复提醒、重要事项提醒或到期延时提醒等,而且提醒方式也支持应用、微信、短信、电话、邮箱及手机系统日历等多种。那这款手机备忘录怎么设置提醒?备忘录提醒

    2024年02月12日
    浏览(30)
  • 手机备忘录如何批量导出来,备忘录整体导出方法介绍

    手机备忘录如何批量导出来?一些品牌的手机上有自带的备忘录,大多手机自带备忘录都不支持直接批量导出,只能通过分享或复制类的功能逐条导出,手动进行整理。 除了手机自带备忘录,一些朋友会选择在自己的手机上使用云便签备忘录,不仅可以在线添加备忘录提醒事

    2024年02月16日
    浏览(37)
  • 苹果备忘录如何转移?备忘录怎么转移到新手机?

    对于很多苹果手机的用户而言,想必都有使用备忘录的习惯吧?但是,经过长期的使用,当需要更换手机设备时,您考虑过如何转移这些记录吗? 苹果备忘录怎么转移到新手机? 如您使用的是苹果手机内置的备忘录,并且需要更换的新设备依然是苹果手机,其解决方案非常

    2024年02月13日
    浏览(41)
  • 苹果手机备忘录如何导入新手机?手机备忘录怎么转移?

    一般来说,大多数手机用户更换手机的频率是3—5年,在一部手机使用了几年之后,就会出现内存不足、系统卡顿、电池续航时间较短等问题,这时候就需要更换新的手机了。有不少苹果手机用户在更换新手机的时候,都很发愁一个问题,这就是手机备忘录如何导入新手机。

    2024年01月25日
    浏览(33)
  • 备忘录模式

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。 1.1 撤销操作: 例如,在文本编辑器中,当我们对文本进行修改时,可以使用备忘录模式来实现撤销操作。每次对文本进行修改时,就保存当

    2024年02月03日
    浏览(32)
  • 备忘录模式(Memento)

    备忘录模式是一种行为设计模式,在不破坏封装性的前提下,允许在不暴露对象实现细节的情况下保存和恢复对象之前的状态。 一个备忘录(memento)是一个对象,它存储另一个对象在某个瞬间的内部状态,而后者称为备忘录的原发器(originator)。当需要设置原发器的检查点时,取

    2024年02月13日
    浏览(27)
  • stata备忘录

    1. 画图 (1)时间趋势图 等价命令 字体大小 option 字体大小option description zero no size whatsoever, vanishingly small minuscule smallest quarter_tiny third_tiny half_tiny tiny vsmall small medsmall medium medlarge large vlarge huge vhuge largest tenth one-tenth the size of the graph quarter one-fourth the size of the graph third one-thi

    2024年02月03日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包