WPF代办事项应用

news2024/9/22 1:53:48

目录

一 设计原型

二 后台源码


一 设计原型

添加代办事项页面:

二 后台源码

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 待办事项应用.DataModel
{
    public class ToDoItem
    {
        public string Description { get; set; } = string.Empty;
        public bool IsChecked {  get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 待办事项应用.DataModel;

namespace 待办事项应用.Services
{
    public class ToDoListService
    {
        public IEnumerable<ToDoItem> GetItems() => new[]
        {
            new ToDoItem{Description="walk the dog" },
            new ToDoItem{Description="buy some milk" },
            new ToDoItem{Description="learn avalonia",IsChecked=true }
        };
    }
}

viewModel:

using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Text;
using System.Threading.Tasks;
using 待办事项应用.DataModel;

namespace 待办事项应用.ViewModels
{
    public class AddItemViewModel : ViewModelBase
    {
        private string _description = String.Empty;
        public ReactiveCommand<Unit, ToDoItem> OkCommand { get; }
        public ReactiveCommand<Unit, Unit> CancelCommand { get; }

        public AddItemViewModel()
        {
            var isValidObservable = this.WhenAnyValue(
                x => x.Description,
                x => !string.IsNullOrWhiteSpace(x));

            OkCommand = ReactiveCommand.Create(
                () => new ToDoItem { Description = Description }, isValidObservable);
            CancelCommand = ReactiveCommand.Create(() => { });
        }

        public string Description
        {
            get => _description;
            set => this.RaiseAndSetIfChanged(ref _description, value);
        }
    }
}
using ReactiveUI;
using System;
using System.Reactive.Linq;
using 待办事项应用.DataModel;
using 待办事项应用.Services;

namespace 待办事项应用.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        private ViewModelBase _contentViewModel;

        // 这个视图模型依赖于 ToDoListService

        public MainWindowViewModel()
        {
            var service = new ToDoListService();
            ToDoList = new ToDoListViewModel(service.GetItems());
            _contentViewModel = ToDoList;
        }

        public ViewModelBase ContentViewModel
        {
            get => _contentViewModel;
            private set => this.RaiseAndSetIfChanged(ref _contentViewModel, value);
        }

        public ToDoListViewModel ToDoList { get; }

        public void AddItem()
        {
            AddItemViewModel addItemViewModel = new();

            Observable.Merge(
                addItemViewModel.OkCommand,
                addItemViewModel.CancelCommand.Select(_ => (ToDoItem?)null))
                .Take(1)
                .Subscribe( newItem =>
                {
                    if (newItem != null)
                    {
                        ToDoList.ListItems.Add(newItem);
                    }
                    ContentViewModel = ToDoList;
                });

            ContentViewModel = addItemViewModel;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 待办事项应用.DataModel;

namespace 待办事项应用.ViewModels
{
    public class ToDoListViewModel : ViewModelBase
    {
        public ObservableCollection<ToDoItem> ListItems { get; set; }

        public ToDoListViewModel(IEnumerable<ToDoItem> items)
        {
            ListItems = new ObservableCollection<ToDoItem>(items);
        }
    }
}
using ReactiveUI;

namespace 待办事项应用.ViewModels
{
    public class ViewModelBase : ReactiveObject
    {
    }
}

View:

<UserControl
    x:Class="待办事项应用.Views.AddItemView"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:待办事项应用.ViewModels"
    d:DesignHeight="450"
    d:DesignWidth="800"
    x:DataType="vm:AddItemViewModel"
    mc:Ignorable="d">
    <DockPanel>
        <Button
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Center"
            Command="{Binding CancelCommand}"
            DockPanel.Dock="Bottom">
            Cancel
        </Button>
        <Button
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Center"
            Command="{Binding OkCommand}"
            DockPanel.Dock="Bottom">
            OK
        </Button>
        <TextBox
            AcceptsReturn="True"
            Text="{Binding Description}"
            Watermark="Enter your to do item" />
    </DockPanel>
</UserControl>
using Avalonia.Controls;

namespace 待办事项应用.Views
{
    public partial class AddItemView : UserControl
    {
        public AddItemView()
        {
            InitializeComponent();
        }
    }
}
<Window
    x:Class="待办事项应用.Views.MainWindow"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="clr-namespace:待办事项应用.Views"
    xmlns:vm="using:待办事项应用.ViewModels"
    Title="待办事项应用"
    d:DesignHeight="450"
    d:DesignWidth="800"
    x:DataType="vm:MainWindowViewModel"
    Content="{Binding ContentViewModel}"
    Icon="/Assets/avalonia-logo.ico"
    mc:Ignorable="d" />
using Avalonia.Controls;

namespace 待办事项应用.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}
<UserControl
    x:Class="待办事项应用.Views.ToDoListView"
    xmlns="https://github.com/avaloniaui"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="using:待办事项应用.ViewModels"
    d:DesignHeight="450"
    d:DesignWidth="800"
    x:DataType="vm:ToDoListViewModel"
    mc:Ignorable="d">
    <DockPanel>
        <Button
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Center"
            x:CompileBindings="False"
            Command="{Binding $parent[Window].DataContext.AddItem}"
            DockPanel.Dock="Bottom">
            Add Item
        </Button>
        <ItemsControl ItemsSource="{Binding ListItems}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <CheckBox
                        Margin="4"
                        Content="{Binding Description}"
                        IsChecked="{Binding IsChecked}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DockPanel>
</UserControl>
using Avalonia.Controls;

namespace 待办事项应用.Views
{
    public partial class ToDoListView : UserControl
    {
        public ToDoListView()
        {
            InitializeComponent();
        }
    }
}

启动引导程序:

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using 待办事项应用.ViewModels;
using 待办事项应用.Views;

namespace 待办事项应用
{
    public partial class App : Application
    {
        public override void Initialize()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override void OnFrameworkInitializationCompleted()
        {
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                desktop.MainWindow = new MainWindow
                {
                    DataContext = new MainWindowViewModel(),
                };
            }

            base.OnFrameworkInitializationCompleted();
        }
    }
}
using Avalonia;
using Avalonia.ReactiveUI;
using System;

namespace 待办事项应用
{
    internal sealed class Program
    {
        // Initialization code. Don't use any Avalonia, third-party APIs or any
        // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
        // yet and stuff might break.
        [STAThread]
        public static void Main(string[] args) => BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);

        // Avalonia configuration, don't remove; also used by visual designer.
        public static AppBuilder BuildAvaloniaApp()
            => AppBuilder.Configure<App>()
                .UsePlatformDetect()
                .WithInterFont()
                .LogToTrace()
                .UseReactiveUI();
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1951005.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构(Java):Map集合Set集合哈希表

目录 1、介绍 1.1 Map和Set 1.2 模型 2、Map集合 2.1 Map集合说明 2.2 Map.Entry<K&#xff0c;V> 2.3 Map常用方法 2.4 Map注意事项及实现类 3、Set集合 3.1 Set集合说明 3.2 Set常用方法 3.3 Set注意事项及其实现类 4、TreeMap&TreeSet 4.1 集合类TreeM…

头歌最小生成树 ------习题

一、背包问题 1.理解&#xff1a;背包问题相当于最小生成树&#xff0c;也就是线性规划最优解 2.公式&#xff1a; M: 背包的总重量 w&#xff1a;物品 i 的重量 p: 物品 i 的价值 3.基本背包练习 4.完全背包问题&#xff1a;每种物品有无限件 >>> 开头加一个for…

面试常考Linux指令

文件权限 操作系统中每个文件都拥有特定的权限、所属用户和所属组。权限是操作系统用来限制资源访问的机制&#xff0c;在 Linux 中权限一般分为读(readable)、写(writable)和执行(executable)&#xff0c;分为三组。分别对应文件的属主(owner)&#xff0c;属组(group)和其他用…

SearchGPT 搜索引擎发布:让信息检索变得简单

如今的互联网时代&#xff0c;我们每天都在与海量数据搏斗。无论是学习、工作还是生活&#xff0c;我们都需要快速准确地获取所需信息。然而&#xff0c;传统搜索引擎往往让人感到力不从心&#xff1a;关键词需要精准&#xff0c;结果泛滥成灾&#xff0c;有用信息如大海捞针。…

如何快速抓取小红书帖子评论?两大实战Python技巧揭秘

摘要&#xff1a; 本文将深入探讨两种高效的Python方法&#xff0c;助您迅速获取小红书文章下方的所有评论&#xff0c;提升市场分析与用户洞察力。通过实战示例与详细解析&#xff0c;让您轻松掌握数据抓取技巧&#xff0c;为您的内容营销策略提供有力支持。 如何快速抓取小…

Linxu系统:hwclock命令

1、命令详解&#xff1a; hwclock命令用于显示与设定硬件时钟。它是一种访问硬件时钟的工具&#xff0c;可以显示当前时间&#xff0c;将硬件时钟设置为指定的时间&#xff0c;将硬件时钟设置为系统时间&#xff0c;以及从硬件时钟设置系统时间。您还可以定期运行hwlock以插入或…

raise JSONDecodeError(“Expecting value”, s, err.value) from None

raise JSONDecodeError(“Expecting value”, s, err.value) from None 目录 raise JSONDecodeError(“Expecting value”, s, err.value) from None 【常见模块错误】 【解决方案】 欢迎来到英杰社区https://bbs.csdn.net/topics/617804998 欢迎来到我的主页&#xff0c;我是…

AI在企业招聘中的应用现状调研报告

2023年&#xff0c;ChatGPT一夜走红&#xff0c;个体陷入了被AI轻易替代的恐慌之中&#xff0c;而企业似乎找到了增长的又一踏板&#xff0c;或被搁置很久或在缓慢开展的「AI」行动又被各行各业提上了日程。 拥抱AI&#xff0c;企业动起来了吗? 从当前的数据来看&#xff0c…

tinygrad框架简介;MLX框架简介

目录 tinygrad框架简介 MLX框架简介 LLaMA​编辑 Stable Diffusion​编辑 tinygrad框架简介 极简主义与易扩展性 tinygrad 的设计理念是极简主义。与 XLA 类比,如果 XLA 是复杂指令集计算 (CISC),那么 tinygrad 就是精简指令集计算 (RISC)。这种简约的设计使得它成为添加…

攻坚克难岁月长,自主腾飞世界强——回顾近代中国数据库的发展与飞跃

前言 最近看了《中国数据库前世今生》纪录片&#xff0c;感触颇深&#xff0c;也是一直在思考到底该用何种方式起笔来回顾这段筚路蓝缕却又充满民族自豪感的历程。大概构思了一周左右吧&#xff0c;我想&#xff0c;或许还是应该从那个计算机技术在国内刚刚萌芽的年代开始讲起…

python+barcode快速生成条形码3-PyQt6微界面(电商条形码生成工具)

背景 继续上一片文章的电商测试小工具&#xff0c;进行了优化 需求 生成条形码之后&#xff0c;可以通过界面方式读取条形码的图片 支持当个条形码快速生成&#xff0c;以及批量导入 csv文件导入 添加微界面图像按钮&#xff0c;方便操作&#xff0c;更像是在实现测试工具的…

开放式耳机会成为未来的主流吗?开放式耳机推荐指南

开放式耳机是否会成为未来的主流&#xff0c;是一个值得探讨的问题。 从目前的市场趋势和技术发展来看&#xff0c;有一些因素支持开放式耳机可能成为主流。 一方面&#xff0c;人们对于健康和舒适的关注度不断提高。长时间佩戴传统耳机可能导致耳部不适&#xff0c;而开放式…

Internet Download Manager2024免费流行的下载加速器

1. Internet Download Manager&#xff08;IDM&#xff09;是一款流行的下载加速器&#xff0c;多线程下载使速度更快。 2. 用户界面友好&#xff0c;易于操作&#xff0c;支持多种浏览器集成和自动捕获下载。 3. 恢复中断的下载&#xff0c;动态文件分割技术提高效率。 4. 定…

解决CORS问题的技术点的原理总结

序言-引出问题 本人在毕业之后主要是从事游戏开发的客户端相关工作&#xff0c;由于游戏引擎的跨平台功能&#xff0c;所以在游戏开发完成之后&#xff0c;需要发布的平台经常会包含Web平台&#xff08;包括desktop Web、Mobile Web&#xff09;。 打包出来的项目文件的入口都…

《书生大模型实战营第3期》入门岛 学习笔记与作业:Python 基础知识

文章大纲 Python 简介1 安装Python1.1 什么是conda&#xff1f;1.1.1 功能与作用&#xff1a;1.1.2 常用命令&#xff1a;1.1.3 适用性&#xff1a; 1.2 Python安装与学习环境准备1.2.1 下载miniconda1.2.2 安装miniconda1.2.3 创建一个python练习专属的conda虚拟环境 2: Pytho…

C++第十弹 ---- vector的介绍及使用

目录 前言vector的介绍及使用1. vector的使用1.1 vector的定义1.2 iterator的使用1.3 vector空间增长问题1.4 vector增删查改 2. vector迭代器失效问题(重点) 总结 前言 本文介绍了C中的vector数据结构及其使用方法。 更多好文, 持续关注 ~ 酷酷学!!! 正文开始 vector的介绍…

【Linux】文件系统|CHS寻址|LBA逻辑块|文件索引|inode|Date block|inodeBitmap|blockBitmap

前言 一个进程通过文件描述符标识一个打开的文件&#xff0c;进程拿着文件描述符可以在内核中找到目标文件进行读写等操作。这是打开的文件&#xff0c;而没有被打开的文件存储在磁盘中&#xff0c;是如何管理的&#xff1f;操作系统在偌大的磁盘中如何找到想要的文件并打开的…

数据传输安全--SSL VPN

目录 IPSEC在Client to LAN场景下比较吃力的表现 SSL VPV SSL VPN优势 SSL协议 SSL所在层次 SSL工作原理 SSL握手协议、SSL密码变化协议、SSL警告协议三个协议作用 工作过程 1、进行TCP三次握手、建立网络连接会话 2、客户端先发送Client HELLO包&#xff0c;下图是包…

目标检测 | YOLO v4、YOLO v5、YOLO v6理论讲解

☀️教程&#xff1a;霹雳吧啦Wz ☀️https://space.bilibili.com/18161609/channel/seriesdetail?sid244160 一、YOLO v4 YOLO v4在2020年的4月发布&#xff0c;YOLO v4结合了大量的前人研究技术加以组合&#xff0c;实现了速度和精度的平衡&#xff0c;该论文包含大量的tric…

二叉树 N0=N2+1

N0 叶子节点&#xff0c;度为 0 的节点&#xff1b; N1 度为 1 的节点&#xff1b; N2 度为 2 的节点 度为 0 的节点为&#xff1a;H、I、J、K、G 度为 1 的节点&#xff1a;E、F 度为 2 的节点&#xff1a;A、B、D、C N0 N2 1&#xff0c;即&#xff1a;度为 0 的叶子节点 …