写点什么

ARTS 打卡第 4 周(9.4~9.10)

作者:向东是大海
  • 2023-09-09
    广东
  • 本文字数:4432 字

    阅读完需:约 15 分钟

A Algorithm 一道算法题

题目:Leetcode 58. 最后一个单词的长度

给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串。

示例:

输入:s = "Hello World"

输出:5

解释:最后一个单词是“World”,长度为 5。

题解:BY C#

public class Solution {    public int LengthOfLastWord(string s) {        int index = s.Length - 1;        while (s[index] == ' ') {            index--;        }        int wordLength = 0;        while (index >= 0 && s[index] != ' ') {            wordLength++;            index--;        }        return wordLength;    }}
复制代码

R Review 读一篇英文文章

原文链接:https://devblogs.microsoft.com/dotnet/wpf-file-dialog-improvements-in-dotnet-8/

WPF File Dialog Improvements in .NET 8

.NET 8 中的 WPF 文件对话框改进

We are thrilled to announce a new set of improvements to the common file dialog API in WPF, starting with .NET 8 Preview 7. This includes the top voted API suggestion in the repository to date – the OpenFolderDialog control to allow users to select a folder – as well as several new properties on file dialogs in general, enabling new user scenarios such as separately persisted states, limiting folder navigation etc.


Until now, WPF supported both Common Item Dialog API introduced in Windows Vista and the legacy GetOpenFileName and GetSaveFileName functions when running on older operating systems. As a part of this update, the dialog code was cleaned up and infrastructure for the legacy functions was removed, since all Windows versions supported by .NET use the newer API only. Applications running in compatibility mode continue to work, but they will present common dialogs using the Common Item Dialog API instead.

我们很高兴地宣布对 WPF 中的通用文件对话框 API 进行一组新的改进,从 .NET 8 预览版 7 开始。这包括迄今为止存储库中投票最多的 API 建议 - 允许用户选择文件夹的 OpenFolderDialog 控件 - 以及文件对话框上的几个新属性,支持新的用户方案,例如单独持久化状态、限制文件夹导航等。


到目前为止,WPF 在较旧的操作系统上运行时,既支持 Windows Vista 中引入的“公共项对话框 API”,也支持旧的 GetOpenFileName 和 GetSaveFileName 函数。作为此更新的一部分,清理了对话框代码并删除了旧函数的基础结构,因为 .NET 支持的所有 Windows 版本仅使用较新的 API。在兼容模式下运行的应用程序将继续工作,但它们将改为使用通用项对话框 API 显示通用对话框。

OpenFolderDialog

One of the most requested feature within the WPF community was a dialog for selecting folders. Example use cases include opening a folder in Visual Studio or Visual Studio Code, saving attachments in Outlook or extracting compressed files into a folder of user’s choice. Until now, developers had to use Windows Forms or rely on third-party libraries to be able to provide this experience, introducing unnecessary dependencies without fitting into the existing model of dialogs.

Starting with .NET 8, we are shipping native support for this dialog in WPF. There has been a lot of discussion in the community on how this feature should be integrated into the existing model of file dialogs, trying to balance compatibility requirements, clean architectural design and underlying API structure. Eventually, we decided to introduce a new base class, CommonItemDialog, into the inheritance chain, where all the common dialog properties got moved.

打开文件夹对话框

最之一 WPF 社区中请求的功能是用于选择文件夹的对话框。示例用例包括在 Visual Studio 或 Visual Studio Code 中打开文件夹、在 Outlook 中保存附件或将压缩文件提取到用户选择的文件夹中。到目前为止,开发人员必须使用 Windows 窗体或依赖第三方库才能提供这种体验,从而引入了不必要的依赖项,而不适合现有的对话框模型。

从 .NET 8 开始,我们将在 WPF 中提供对此对话框的本机支持。社区中有很多关于如何将此功能集成到现有文件对话框模型中的讨论,试图平衡兼容性要求、干净的架构设计和底层 API 结构。最终,我们决定在继承链中引入一个新的基类 CommonItemDialog,其中所有公共对话框属性都被移动。

New Dialog Properties

We also expanded the number of properties to configure the behavior of file dialogs in WPF, covering as much as currently possible of the underlying API. To highlight some of them:

ClientGuid identifies the dialog’s persistent state. This allows Windows to remember dialog state such as window size or last used folder separately per dialog, for example for the Save and Save As dialogs.

Setting AddToRecent to false instructs the dialog to not add the opened or saved file or folder to the most recent items list maintained by Windows for the user. This can be for example used to prevent a configuration file from appearing in the Recommended section of the Start menu.

Setting CreateTestFile to false prevents SaveFileDialog to verify user has access to the selected location by creating and deleting a dummy file. This can be useful when access to the location is expected to be expensive. However, the application will have to do all the appropriate error handling when creating the file itself.

RootDirectory limits the folder tree in the dialog to a certain folder and its subfolders.

新建对话框属性

我们还扩展了属性的数量,以配置 WPF 中文件对话框的行为,尽可能多地涵盖当前的基础 API。要突出显示其中的一些:

客户端 Guid 标识对话框的持久状态。这允许 Windows 为每个对话框分别记住对话框状态,例如窗口大小或上次使用的文件夹,例如“保存”和“另存为”对话框。

将“AddToRecently”设置为 false 指示对话框不将打开或保存的文件或文件夹添加到 Windows 为用户维护的最新项目列表中。例如,这可以用于防止配置文件出现在“开始”菜单的“推荐”部分中。

将 CreateTestFile 设置为 false 可防止 SaveFileDialog 通过创建和删除虚拟文件来验证用户是否有权访问所选位置。当访问该位置预计会很昂贵时,这可能很有用。但是,应用程序在创建文件本身时必须执行所有适当的错误处理。

RootDirectory 将对话框中的文件夹树限制为特定文件夹及其子文件夹。

What’s Next for File Dialogs

There is still room for improvement regarding the file dialogs, such as supporting virtual files or customizing the dialog by including additional controls in the user interface. We have a pull request from the community proposing an implementation of the file dialog controls and we would like to invite everyone interested to participate in shaping this proposal and help us prioritize features that matter to you and your applications.

文件对话框的下一步是什么

文件对话框仍有改进的余地,例如支持虚拟文件或通过在用户界面中包含其他控件来自定义对话框。我们收到了来自社区的拉取请求,提议实现文件对话框控件,我们希望邀请所有感兴趣的人参与制定此提案,并帮助我们确定对您和您的应用程序重要的功能的优先级。

T Technique/Tip 分享一个小技术

C# 和 OpenResty 中进行 CRC32

1、C# 进行 CRC32

public class CRC32{    private static readonly uint[] _crc32Table;    static CRC32()    {        uint crc;        _crc32Table = new uint[256];        int i, j;        for (i = 0; i < 256; i++)        {            crc = (uint)i;            for (j = 8; j > 0; j--)            {                if ((crc & 1) == 1)                    crc = (crc >> 1) ^ 0xEDB88320;                else                    crc >>= 1;            }            _crc32Table[i] = crc;        }    }
/// <summary> /// 获取CRC32校验值 /// </summary> public static uint GetCRC32(byte[] bytes) { uint value = 0xffffffff; int len = bytes.Length; for (int i = 0; i < len; i++) { value = (value >> 8) ^ _crc32Table[(value & 0xFF) ^ bytes[i]]; } return value ^ 0xffffffff; }
/// <summary> /// 获取CRC32校验值 /// </summary> public static uint GetCRC32(string str) { byte[] bytes = Encoding.UTF8.GetBytes(str); return GetCRC32(bytes); }}
复制代码

使用方法

string dataStr = "1234567890";

var crcUint = CRC32.GetCRC32(dataStr);

var crcHex = string.Format("{0:X8}", crcUint);

Console.WriteLine($"{dataStr} => CRC32 Uint: {crcUint}, Hex: {crcHex}");

结果:1234567890 => CRC32 Uint: 639479525, Hex: 261DAEE5


2、OpenResty 中进行 CRC32

location /lua_crc {    content_by_lua_block    {        local str = "1234567890"        local crc32_long =  ngx.crc32_long(str)        ngx.say(str .. " => CRC32 long: " .. crc32_long, "</br>")    }}
复制代码

结果:1234567890 => CRC32 long: 639479525


3、C# 和 OpenResty 中进行 CRC32 的结果是一致的。

S Share 分享一个观点

俗话说,万事开头难。你是不是,在开始一项工作、开始一段学习时,总是迟迟不能开始?你是不是计划要做很多事情,可到头来一件事也没有做?这里有克服拖延症的几个好用的方法

1、5 分钟起步法。开始做一件事的时候,先给自己 5 分钟的起步时间,比如先开始读一页书,或者背 10 个单词,别计较效率。接受自己刚开始那 5 分钟的烦躁和抗拒,继续做该做的事情,之后就好了。因为,真正能够影响行动的,是行动本身。不是说等我想做了,我才能去做;而是我先开始做,我就会进入想做的状态。

2、稍微走在计划前面。给自己留下偷懒的资格和空间,当你相对拥有不做事的权利的时候,你才会更愿意追求主动做事。

3、替代拖延法。如果有一个更难的任务需要完成,我们就非常极其愿意去做相对简单的任务了。我们可以利用这种心理,在拖延艰难任务的过程中,把容易的任务迅速解决掉。

用户头像

先精之,再思之,五六分把握即做之。 2020-06-24 加入

还未添加个人简介

评论

发布
暂无评论
ARTS 打卡第 4 周(9.4~9.10)_ARTS 打卡计划_向东是大海_InfoQ写作社区