C# 我怎样才能在一段时间后隐藏旋转器?
我想放一个旋转器加载,过一会儿(大约3,4秒)隐藏它。我怎么做C# 我怎样才能在一段时间后隐藏旋转器?,c#,wpf,spinner,C#,Wpf,Spinner,我想放一个旋转器加载,过一会儿(大约3,4秒)隐藏它。我怎么做 <StackPanel Grid.ColumnSpan="5" Grid.RowSpan="10" Background="White"Name="spinner"> <fa:ImageAwesome Width="80" Icon="Spinner" Spin="True" SpinDuration="2" /> </StackPanel> 在XAML中设置元素的x:Name
<StackPanel Grid.ColumnSpan="5" Grid.RowSpan="10" Background="White"Name="spinner">
<fa:ImageAwesome Width="80" Icon="Spinner" Spin="True" SpinDuration="2" />
</StackPanel>
在XAML中设置元素的
x:Name
属性:
<fa:ImageAwesome x:Name="MyIcon" Width="80" Icon="Spinner" Spin="True" SpinDuration="2" />
这是一个纯XAML解决方案,没有任何代码隐藏:
private DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
//Create a timer with interval of 3 secs
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 3);
MyIcon.Visibility = System.Windows.Visibility.Visible;
// Start the timer
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
MyIcon.Visibility = System.Windows.Visibility.Collapsed;
// Stop the timer
dispatcherTimer.Stop();
}
<StackPanel ...>
<StackPanel.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:3"
Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</StackPanel.Triggers>
<fa:ImageAwesome .../>
</StackPanel>
请注意,您应该调用Start和Stop,或者将IsEnabled设置为true和false。两者混合看起来很奇怪。完美!谢谢