简体   繁体   English

为什么MahApps.Metro ShowProgressAsync对话框意外重绘? (总是灰色的)

[英]Why does MahApps.Metro ShowProgressAsync dialog box redraw unexpectedly? (always grey)

So, I'm guessing a bit, but it looks like my Mahapps.Metro ShowProgressAsync dialog box is redrawing very quickly, so it always looks greyed out. 所以,我猜测了一下,但看起来我的Mahapps.Metro ShowProgressAsync对话框正在快速重绘,因此它总是显得灰暗。

I have a program that is looking through a document for certain matches based on regex, and I've set up a progress bar, but the dialog just greys out the main application, and then just shows the dialog as grey (like it's loading it over and over very quickly, or freezing). 我有一个程序正在查看基于正则表达式的某些匹配的文档,我已经设置了一个进度条,但是对话框只是显示主应用程序,然后只是将对话框显示为灰色(就像它正在加载它一样)反复很快,或冻结)。

If I put some sort of stop in there, like a messagebox, then everything shows fine. 如果我在那里放置某种停止,就像一个消息框,那么一切都很好。 I don't think my code should be redrawing the dialog every time. 我不认为我的代码每次都应该重新绘制对话框。 I thought it should only be updating the progress bar. 我认为它应该只是更新进度条。 Here's my code. 这是我的代码。

In this example code, instead of showing the logic where I add a page number to a list, I just added the number 42 over and over, just to make it shorter 在这个示例代码中,我没有显示我在列表中添加页码的逻辑,而是一遍又一遍地添加了数字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();
    }

Your Operation need to be on a different Thread: 您的Operation需要在不同的线程上:

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();
}

I dont know if this is intentional but this Method does a "Fire and Forget" async void . 我不知道这是否是故意的,但是这个方法做了“Fire and Forget” async void I would suggest to change the Method signature to async task to await it on the other side. 我建议将Method签名更改为async task以在另一侧等待它。 Further more the Exception will be handled that way : Exception Handling 此外,Exception将以这种方式处理: 异常处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM