2011-11-23 106 views
2

我是WPF的初學者。 我想在每個打勾上顯示我的表格。 但它只顯示一次。 當我調試這個,它在計時器滴答事件中打this.topmost=true,但是 它沒有顯示窗口。 我不確定這段代碼有什麼問題。wpf中的動畫

public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); 

       timer.Interval = new TimeSpan(0, 0, 0, 2, 0); 

       timer.Tick += tick;     

       timer.Start();     

     } 
     private void tick(object sender, EventArgs e) 
     { 

      this.Topmost = true;//display the form 
      this.Show(); 

     } 
    } 

<Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10"> 
     <StackPanel Margin="20"> 

      <CheckBox Content="Checkable" Margin="5 5 0 5" /> 
      <Button Content="Clickable" HorizontalAlignment="Center" /> 
     </StackPanel> 
    </Border> 

<!-- Animation --> 
     <Grid.Triggers> 
      <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
       <BeginStoryboard> 
        <Storyboard> 
         <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"> 
          <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> 
          <SplineDoubleKeyFrame KeyTime="0:0:0.0" Value="1"/> 
         </DoubleAnimationUsingKeyFrames> 
         <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"> 
          <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/> 
          <SplineDoubleKeyFrame KeyTime="0:0:8" Value="0"/> 
         </DoubleAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Grid.Triggers> 
+0

我對你想要做的事情有點困惑。表單正在初始化並在InitializeComponent方法中顯示。你所有的定時器正在做的是使它成爲最頂層的窗口。它現在已經可見了。 –

+0

一個完全無關的建議(因爲你提到你是WPF的新手),而不是使用'新TimeSpan(0,0,0,2,0)',使用'TimeSpan.FromSeconds(2)'。它使代碼更易於閱讀:) –

回答

0

這應該做你想要什麼。我已將您想要展示給客戶的WPF窗口轉移到另一個窗體中。然後,我創建了另一個隱藏的啓動窗體,並定期運行定時器以顯示您的動畫。

啓動窗體:

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="10" Width="10" ShowInTaskbar="False" Visibility="Hidden" > 

    <Grid /> 

</Window> 

啓動窗體代碼隱藏

public partial class MainWindow : Window 
{ 

    Window1 cyclicWindow; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     cyclicWindow = new Window1(); 
     cyclicWindow.Show(); 
     cyclicWindow.Topmost = true; 
     System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer(); 
     timer.Interval = new TimeSpan(0, 0, 0, 20, 0); 
     timer.Tick += tick; 
     timer.Start(); 
    } 

    private void tick(object sender, EventArgs e) 
    { 

     if (cyclicWindow != null) 
     { 
      cyclicWindow.Close() ; 
     } 

     cyclicWindow = new Window1(); 
     cyclicWindow.Show(); 
     cyclicWindow.Topmost = true; 

    } 

} 

自行車窗口:

<Window x:Class="WpfApplication1.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" Height="200" Width="300" Name="Main" 
     ShowInTaskbar="False" WindowStyle="None" Background="Transparent" > 

    <Grid Name="Base" Height="112"> 
     <Border BorderThickness="1" Background="Beige" BorderBrush="Black" CornerRadius="10"> 
      <StackPanel Margin="20"> 

       <CheckBox Content="Checkable" Margin="5 5 0 5" /> 
       <Button Content="Clickable" HorizontalAlignment="Center" /> 
      </StackPanel> 
     </Border> 

     <!-- Animation --> 
     <Grid.Triggers> 
      <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
       <BeginStoryboard> 
        <Storyboard> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Main" Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"> 
          <SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/> 
          <SplineDoubleKeyFrame KeyTime="0:0:0.0" Value="1"/> 
         </DoubleAnimationUsingKeyFrames> 
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="Main" Storyboard.TargetProperty="(UIElement.Opacity)"> 
          <SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/> 
          <SplineDoubleKeyFrame KeyTime="0:0:8" Value="0"/> 
         </DoubleAnimationUsingKeyFrames> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger> 
     </Grid.Triggers> 
    </Grid> 
</Window> 

自行車窗口代碼背後:

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 
     AllowsTransparency = true; 
    } 
} 
+0

非常感謝您的回覆。 我希望此窗口在用戶登錄後每20分鐘彈出一次,並且不會永久自動隱藏。 –

+0

@LillyW我已經調整了我的答案,以說明您在評論中提供的其他信息。它是做你想做的嗎? –

+0

非常感謝您的評價我非常感謝。 –