【问题标题】:Show a sequence of images in WPF then close form在 WPF 中显示一系列图像,然后关闭表单
【发布时间】:2019-08-13 11:21:59
【问题描述】:

我有一个显示打开锁的窗口。 当用户单击按钮时,锁必须变为关闭状态,稍等片刻,然后关闭窗口。

如何使用 WPF 做到这一点?

这是我最初的 xaml:

<Button Grid.Row="2" BorderThickness="0" Background="Transparent" Margin="32"
       IsTabStop="False" Click="BtnUnlockClick">
    <Button.Content>
        <Grid>
            <Image Grid.Row="1" Source="/Common.Wpf;component/images/unlocked.png" Visibility="Visible" Name="imgUnlocked"/>
            <Image Grid.Row="1" Source="/Common.Wpf;component/images/locked.png" Visibility="Collapsed" Name="imgLocked"/>
        </Grid>                
    </Button.Content>
</Button>  

和 C#:

private void BtnUnlockClick(object sender, RoutedEventArgs e)
{
    //do stuff here
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    仅将一个 Image 元素放入 Button 的内容中。

    <Button Click="BtnUnlockClick" ...>
        <Image Source="/Common.Wpf;component/images/unlocked.png"/>
    </Button>  
    

    在 Click 事件处理程序中更改其源,稍等片刻,然后关闭窗口。处理程序方法必须声明为async

    private async void BtnUnlockClick(object sender, RoutedEventArgs e)
    {
        var image = (Image)((Button)sender).Content;
        image.Source = new BitmapImage(
            new Uri("pack://application:,,,/Common.Wpf;component/images/locked.png"));
    
        await Task.Delay(1000);
    
        Close();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多