2016-04-23 77 views
1

我試圖實現一個登錄按鈕,該按鈕一旦被按下就變爲請稍候...模式。我正在考慮按鈕或GIF圖像動畫中的一些StoryBoard動畫。似乎我對WPF很新,經過多次搜索嘗試後,我無法找到完整的工作思路。WPF按鈕gif動畫

登錄按鈕想法
enter image description here

什麼會做上述事情的最好方法?有什麼方法可以消除GIF圖像並使用一些Path或類似的東西創建相同的圖像?另外請注意,我希望在Button.Click事件中觸發按鈕的這種行爲。

+0

如果沒有帶有一個靜態圖像和「RotateTransform」故事板的第三方庫,您可以輕鬆完成。點擊時激活多久? – dkozl

+0

最後我用第三方GIF插件做了它。 – Anup

回答

1

只需在您的視覺工作室中添加WpfAnimatedGifTools -> NuGet Package Manager->Package Manager Console.在那裏您可以找到包裝添加器窗口並鍵入pm > Install-Package WpfAnimatedGif.幾秒鐘後應該添加包裝。現在首先,你必須添加namespacexmlns:gif="http://wpfanimatedgif.codeplex.com"然後設計你的Button.

<Window x:Class="WpfApplication1.MainWindow" Name="root" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:gif="http://wpfanimatedgif.codeplex.com" 
      Width="500" Height="500" DataContext="{Binding ElementName=root}"> 

     <StackPanel> 
      <Button Name="BtnLogin" Width="100" Height="30" Content="Login" Background="Red" Margin="0,0,0,5" Click="Button_Click"/> 
      <Button Width="100" Height="30" Background="Red"> 
      <Grid> 
        <Image gif:ImageBehavior.AnimatedSource="{Binding DataContext.LoadingImage}" HorizontalAlignment="Left" VerticalAlignment="Center"/> 
       <TextBlock Text="Please Wait" Padding="30,0,0,0" HorizontalAlignment="Right" VerticalAlignment="Center"/> 
      </Grid> 
     </Button> 
     </StackPanel> 
     </Window> 

以下cs文件是下面,當你需要停止加載圖像,只是分配LoadingImage = null;

public partial class MainWindow : Window,INotifyPropertyChanged 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
     private ImageSource _LoadingImage; 
     public ImageSource LoadingImage 
     { 
      get { return _LoadingImage; } 
      set 
      { 
       _LoadingImage = value; 
       OnPropertyChanged("LoadingImage"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      LoadingImage = GetBitmapImage("/aaaa.gif"); 
     } 
     public static BitmapImage GetBitmapImage(String location) 
     { 
      BitmapImage image = null; 
      try 
      { 
       Uri iconUri = new Uri("pack://application:,,,/" + ";component" + location, UriKind.RelativeOrAbsolute); 

       image = new BitmapImage(iconUri); 
      } 
      catch (Exception ex) 
      { 
      } 
      return image; 
     } 
    } 
+0

以上代碼將始終顯示gif。我只希望它在Click事件。 – Anup

+0

@Anup你能指出你是如何解決它的?我對它感興趣。請分享您的解決方案。謝謝。 – user1624552

+0

@ user1624552如答案中所述,我遵循相同的步驟,即WpfAnimateGif nuget包。請注意,我沒有選擇更改按鈕文本。相反,我在單獨的控件中顯示了動畫gif和文本。 – Anup