Fügen Sie einfach WpfAnimatedGif
von Ihrem Visual Studio Tools -> NuGet Package Manager->Package Manager Console.
Dort finden Sie das Paket Addierfenster und geben Sie pm > Install-Package WpfAnimatedGif.
Nach ein paar Sekunden sollte das Paket hinzugefügt werden. Nun erstens haben Sie namespace
hinzufügen xmlns:gif="http://wpfanimatedgif.codeplex.com"
dann gestalten Sie Ihre 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>
Die folgende CS-Datei unten ist, und wenn Sie Laden Bild anhalten müssen, nur zuweisen 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;
}
}
Sie können es ohne Bibliotheken von Drittanbietern mit einem statischen Bild und 'RotateTransform' Storyboard leicht tun. Aktiviere auf Klick, aber für wie lange? – dkozl
Endlich habe ich es mit dem GIF-Plugin von Drittanbietern gemacht. – Anup