我正在尝试实现一个登录按钮,一旦按下该按钮就会变为请等待...模式。我正在考虑按钮或GIF图像动画中的一些StoryBoard动画。似乎我对WPF很陌生,经过几次搜索尝试,我无法找到一个完全有效的想法。
做上述事情的最佳方法是什么?有没有办法消除GIF图像,并使用一些路径或类似的东西创建相同的?另请注意,我希望在Button.Click事件上触发按钮的这种行为。
答案 0 :(得分:2)
只需在视觉工作室WpfAnimatedGif
添加Tools -> NuGet Package Manager->Package Manager Console.
即可找到包加法器窗口并输入pm > Install-Package WpfAnimatedGif.
几秒后,应添加包。首先,您已添加namespace
xmlns: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;
}
}