C# 为什么MahApps.Metro ShowProgressAsync对话框意外重画?(始终为灰色)

C# 为什么MahApps.Metro ShowProgressAsync对话框意外重画?(始终为灰色),c#,wpf,async-await,mahapps.metro,C#,Wpf,Async Await,Mahapps.metro,所以,我猜了一下,但它看起来像是我的Mahapps.Metro ShowProgressAsync对话框正在快速重画,所以它看起来总是灰色的 我有一个程序,它在一个文档中查找基于正则表达式的某些匹配项,我设置了一个进度条,但是对话框只是将主应用程序显示为灰色,然后将对话框显示为灰色(就像它很快地反复加载,或者冻结) 如果我在那里放了一些止损点,比如一个消息框,那么一切都会很好。我认为我的代码不应该每次都重新绘制对话框。我想它应该只更新进度条。这是我的密码 在这个示例代码中,我没有显示向列表中添加

所以,我猜了一下,但它看起来像是我的Mahapps.Metro ShowProgressAsync对话框正在快速重画,所以它看起来总是灰色的

我有一个程序,它在一个文档中查找基于正则表达式的某些匹配项,我设置了一个进度条,但是对话框只是将主应用程序显示为灰色,然后将对话框显示为灰色(就像它很快地反复加载,或者冻结)

如果我在那里放了一些止损点,比如一个消息框,那么一切都会很好。我认为我的代码不应该每次都重新绘制对话框。我想它应该只更新进度条。这是我的密码

在这个示例代码中,我没有显示向列表中添加页码的逻辑,而是反复添加数字42,以使其更短

    private async void RegexMatchProgressBar(Regex regex, string myText, Microsoft.Office.Interop.Word.Document myDoc)
    {
        int charCount = myDoc.Application.ActiveDocument.Characters.Count;

        var myProgressAsync = await this.ShowProgressAsync("WAIT WHILE WE DO STUFF!", "Searching...");
        myProgressAsync.Maximum = charCount;
        myProgressAsync.Minimum = 0;

        Dictionary<String, List<int>> table = new Dictionary<string, List<int>>();
        foreach (Match match in regex.Matches(myText))
        {
            if (!table.ContainsKey(match.Value))
            {
                List<int> page = new List<int>();
                page.Add(42);
                table.Add(match.Value, page);
                myProgressAsync.SetProgress((double)match.Index);

            }
        }
        myProgressAsync.SetProgress(charCount);
        await myProgressAsync.CloseAsync();
    }
private async void RegexMatchProgressBar(Regex Regex,string myText,Microsoft.Office.Interop.Word.Document myDoc)
{
int charCount=myDoc.Application.ActiveDocument.Characters.Count;
var myProgressAsync=等待这个。ShowProgressAsync(“在我们做事时等待!”,“搜索…”);
最大值=字符数;
myProgressAsync.Minimum=0;
字典表=新字典();
foreach(regex.Matches中的匹配(myText))
{
如果(!table.ContainsKey(match.Value))
{
列表页=新列表();
第页.添加(42);
表.添加(match.Value,第页);
myProgressAsync.SetProgress((双精度)match.Index);
}
}
myProgressAsync.SetProgress(charCount);
等待myProgressAsync.CloseAsync();
}

您的
操作需要位于不同的线程上:

private async void RegexMatchProgressBar(Regex regex, string myText, Microsoft.Office.Interop.Word.Document myDoc)
{
    int charCount = myDoc.Application.ActiveDocument.Characters.Count;

    var myProgressAsync = await this.ShowProgressAsync("WAIT WHILE WE DO STUFF!", "Searching...");
    myProgressAsync.Maximum = charCount;
    myProgressAsync.Minimum = 0;

    await Task.Run(() => 
    {
        Dictionary<String, List<int>> table = new Dictionary<string, List<int>>();
        foreach (Match match in regex.Matches(myText))
        {
            if (!table.ContainsKey(match.Value))
            {
                List<int> page = new List<int>();
                page.Add(42);
                table.Add(match.Value, page);
                myProgressAsync.SetProgress((double)match.Index);

            }
        }

        myProgressAsync.SetProgress(charCount);
    });

    await myProgressAsync.CloseAsync();
}
private async void RegexMatchProgressBar(Regex Regex,string myText,Microsoft.Office.Interop.Word.Document myDoc)
{
int charCount=myDoc.Application.ActiveDocument.Characters.Count;
var myProgressAsync=等待这个。ShowProgressAsync(“在我们做事时等待!”,“搜索…”);
最大值=字符数;
myProgressAsync.Minimum=0;
等待任务。运行(()=>
{
字典表=新字典();
foreach(regex.Matches中的匹配(myText))
{
如果(!table.ContainsKey(match.Value))
{
列表页=新列表();
第页.添加(42);
表.添加(match.Value,第页);
myProgressAsync.SetProgress((双精度)match.Index);
}
}
myProgressAsync.SetProgress(charCount);
});
等待myProgressAsync.CloseAsync();
}
我不知道这是否是故意的,但此方法会执行“激发并忘记”
async void
。我建议将方法签名更改为
async task
,以便在另一端等待它。此外,将以这种方式处理异常: