Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解决显示弹出窗口的问题?_C#_Wpf_Xaml - Fatal编程技术网

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

C# 如何解决显示弹出窗口的问题?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个弹出窗口,每当用户键入不允许的字符时就会出现,然后我在屏幕上延迟弹出窗口6秒,我想做的是,当用户键入允许的字符时,弹出窗口应该立即消失,当用户再次键入不允许的字符时,如果用户没有键入任何内容,它应该显示弹出窗口,然后等待6秒钟。我是这样做的,但它不起作用,弹出窗口显示,然后如果我键入允许的字符,它会将IsOpen设置为false,但当用户再次键入不允许的字符时,延迟不再起作用,它会在随机秒内关闭弹出窗口。我如何解决这个问题,或者更好的解决方法是什么 private async v

我有一个弹出窗口,每当用户键入不允许的字符时就会出现,然后我在屏幕上延迟弹出窗口6秒,我想做的是,当用户键入允许的字符时,弹出窗口应该立即消失,当用户再次键入不允许的字符时,如果用户没有键入任何内容,它应该显示弹出窗口,然后等待6秒钟。我是这样做的,但它不起作用,弹出窗口显示,然后如果我键入允许的字符,它会将
IsOpen
设置为false,但当用户再次键入不允许的字符时,延迟不再起作用,它会在随机秒内关闭弹出窗口。我如何解决这个问题,或者更好的解决方法是什么

    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;
        }
    }
private async void txtName\u TextChanged(对象发送方,事件参数e)
{
if(Regex.IsMatch(txtName.Text,@“[\\/:*?”“\124;]”)
{
字符串模式=@“[\\/:*?”“\;]”;
正则表达式正则表达式=新正则表达式(模式);
txtName.Text=regex.Replace(txtName.Text,“”);
如果(AlertPopup.IsOpen==false)
{
AlertPopup.IsOpen=true;
等待任务。延迟(6000);//它保持为真6秒
AlertPopup.IsOpen=false;
}
}
其他的
{
AlertPopup.IsOpen=false;
}
}
这是弹出窗口的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
接受一个
CancellationToken
,您可以在每个按键笔划上使用取消任务。大概是这样的:

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;
    }
}
private CancellationTokenSource cts;
私有异步void txtName\u TextChanged(对象发送方,事件参数e)
{
如果(cts!=null)
{
cts.Cancel();
cts.Dispose();
}
cts=新的CancellationTokenSource();
if(Regex.IsMatch(txtName.Text,@“[\\/:*?”“\124;]”)
{
字符串模式=@“[\\/:*?”“\;]”;
正则表达式正则表达式=新正则表达式(模式);
txtName.TextChanged-=txtName\u TextChanged;
txtName.Text=regex.Replace(txtName.Text,“”);
txtName.TextChanged+=txtName\u TextChanged;
如果(AlertPopup.IsOpen==false)
{
AlertPopup.IsOpen=true;
尝试
{
等待任务延迟(6000,cts.Token);
}
catch(TaskCanceledException){}
最后
{
AlertPopup.IsOpen=false;
}
}
}
其他的
{
AlertPopup.IsOpen=false;
}
}

可能是任务。第一次调用的延迟仍在运行,并最终关闭弹出窗口。因此,您需要取消此延迟,以便正确识别文本。我会使用一个调度器,这样可以更容易地取消。