小编hel*_*rse的帖子

错误:未定义或导入预定义类型“System.Range”

早些时候我收到错误:

Feature 'range operator' is not available in C# 7.3. Please use language version 8.0 or greater.

当我升级项目时,仍然存在以下错误:

Predefined type 'System.Range' is not defined or imported

基本上尝试测试我们如何检索数组或字符串的片段:

using System;
using System.Windows.Forms;

namespace TestRange
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var story = "Testing C# 8";
            Console.WriteLine(story[^6..^0]); // (last 6 chars)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我参考过但没有帮助的文章:

https://learn.microsoft.com/en-us/dotnet/api/system.range?view=netcore-3.1 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals /csharp-8.0/ranges https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes

https://www.infoworld.com/article/3532284/how-to-use-indices-and-ranges-in-csharp-80.html

https://www.codejourney.net/2019/02/csharp-8-slicing-indexes-ranges/

一个相关问题:C# 中的数组切片

.net c# range slice

5
推荐指数
1
解决办法
3031
查看次数

使用 async 和 await 实时更新数据

我已经尝试了这个答案中提到的代码:https : //stackoverflow.com/a/27089652/

它工作正常,我想用它在 for 循环中运行 PowerShell 脚本。GUI最初冻结然后我尝试了这个答案中提到的代码:https : //stackoverflow.com/a/35735760/

现在,当 PowerShell 脚本在后台运行时,GUI 不会冻结,尽管在 for 循环完成之前文本框中没有任何更新。我想看到实时更新的结果。这是我正在运行的代码:

        private async void run_click(object sender, RoutedEventArgs e)
        {
            Text1.Text = "";
            
            await Task.Run(() => PS_Execution(Text1));

        }

        internal async Task PS_Execution(TextBox text)
        {

            PowerShell ps = PowerShell.Create();
            ps.AddScript(script.ToString());

            {

                Collection<PSObject> results = ps.Invoke();
                foreach (PSObject r in results)
                {
                    text.Dispatcher.Invoke(() =>
                    {
                        text.Text += r.ToString();
                    });
                    await Task.Delay(100);
                }                
            }
        }
Run Code Online (Sandbox Code Playgroud)

也许我错过了一些重要的东西。请帮助我了解如何解决这个问题。

wpf powershell async-await

1
推荐指数
1
解决办法
83
查看次数

标签 统计

.net ×1

async-await ×1

c# ×1

powershell ×1

range ×1

slice ×1

wpf ×1