简体   繁体   English

如何解决显示弹出窗口的问题?

[英]How can I fix this problem of showing the popup?

I have a popup that shows up whenever the user types unallowed characters, then I put a delay to hold the popup on the screen for 6 seconds, and what I want to do is that when the user types the allowed characters, then the popup should immediately disappear, and when the user types the unallowed chars again, it should show the popup and then wait for 6 seconds if the user didn't type anything.我有一个弹出窗口,每当用户键入不允许的字符时就会出现,然后我延迟将弹出窗口在屏幕上保持 6 秒,我想要做的是当用户键入允许的字符时,弹出窗口应该立即消失,当用户再次输入不允许的字符时,它应该显示弹出窗口,如果用户没有输入任何内容,然后等待 6 秒。 I've done it this way, But it's not working, the popup shows up, and then if I type an allowed char, it set the IsOpen to false, but when the user types the unallowed chars again, the delay is not working anymore, it closes the popup in random seconds.我已经这样做了,但它不起作用,弹出窗口显示,然后如果我输入一个允许的字符,它将IsOpen设置为 false,但是当用户再次输入不允许的字符时,延迟不再起作用,它会在随机几秒钟内关闭弹出窗口。 How can I fix this problem, or what's the better way of doing this?我该如何解决这个问题,或者更好的方法是什么?

    private async void txtName_TextChanged(object sender, EventArgs e)
    {
        if (Regex.IsMatch(txtName.Text, @"[\\/:*?""<>|]"))
        {
            string pattern = @"[\\/:*?""<>|]";
            Regex regex = new Regex(pattern);
            txtName.Text = regex.Replace(txtName.Text, "");

            if (AlertPopup.IsOpen == false)
            {
                AlertPopup.IsOpen = true;
                await Task.Delay(6000); //It stays true for 6 seconds
                AlertPopup.IsOpen = false;
            }
        }
        else
        {
            AlertPopup.IsOpen = false;
        }
    }

This is the XAML code for popup:这是弹出窗口的 XAML 代码:

    <Popup AllowsTransparency="True" PlacementTarget="{Binding ElementName=txtName}"
           Placement="Bottom" x:Name="AlertPopup">
        <Border Margin="0,0,35,35" Background="#272C30" BorderBrush="#6C757D"
                BorderThickness="2" CornerRadius="10">
            <Border.Effect>
                <DropShadowEffect Color="Black" BlurRadius="35" Direction="315"
                                  ShadowDepth="16" Opacity="0.2"/>
            </Border.Effect>
            <TextBlock Padding="8,3,8,5" Foreground="#ADB5BD" Background="Transparent" FontSize="12"
            Text="The file name can't contain any of the following&#x0a;characters :   \  / : * ? &quot; &lt; &gt; |"/>
        </Border>
    </Popup>

Task.Delay accepts a CancellationToken that you can use the cancel the task on each key stroke. Task.Delay接受一个CancellationToken ,您可以在每次击键时使用取消任务。 Something like this:像这样的东西:

private CancellationTokenSource cts;
private async void txtName_TextChanged(object sender, EventArgs e)
{
    if (cts != null)
    {
        cts.Cancel();
        cts.Dispose();
    }
    cts = new CancellationTokenSource();

    if (Regex.IsMatch(txtName.Text, @"[\\/:*?""<>|]"))
    {
        string pattern = @"[\\/:*?""<>|]";
        Regex regex = new Regex(pattern);
        txtName.TextChanged -= txtName_TextChanged;
        txtName.Text = regex.Replace(txtName.Text, "");
        txtName.TextChanged += txtName_TextChanged;

        if (AlertPopup.IsOpen == false)
        {
            AlertPopup.IsOpen = true;
            try
            {
                await Task.Delay(6000, cts.Token);
            }
            catch (TaskCanceledException) { }
            finally
            {
                AlertPopup.IsOpen = false;
            }
        }
    }
    else
    {
        AlertPopup.IsOpen = false;
    }
}

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

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