我想在标签上做一系列动画,例如,首先从值0到1执行不透明度动画,反之亦然,只是在不透明度动画结束时,而不是在前景动画之前.我想在XAML代码中执行它,然后从C#代码开始和完成de动画.这是最好和最有效的方法吗?
欢迎所有回复!
提前致谢.
我有一个需要很长时间才能执行的任务.为了通知用户进度,我有一个我在里面更新的进度条DoWork.
有人能告诉我这是否是更新进度条的最佳方法?我听说有一个ReportProgress事件处理程序,但我很困惑,因为我不确定的目的ReportProgress.
我有一个主WPF窗口,其中一个控件是我创建的用户控件.此用户控件是模拟时钟,包含更新时针,分针和秒针的线程.最初它不是一个线程,它是一个更新小时,分钟和秒的计时器事件,但我已将其更改为线程,因为当用户按下开始按钮然后时钟不会时应用程序做了一些努力工作更新,所以我把它改成了一个线程.
WPF窗口的代码片段:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GParts"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes
assembly=PresentationFramework.Aero"
xmlns:UC="clr-namespace:GParts.UserControls"
x:Class="GParts.WinMain"
Title="GParts"
WindowState="Maximized"
Closing="Window_Closing"
Icon="/Resources/Calendar-clock.png"
x:Name="WMain"
>
<...>
<!-- this is my user control -->
<UC:AnalogClock Grid.Row="1" x:Name="AnalogClock" Background="Transparent"
Margin="0" Height="Auto" Width="Auto"/>
<...>
</Window>
Run Code Online (Sandbox Code Playgroud)
我的问题是,当用户退出应用程序时,线程似乎继续执行.我希望线程在主窗口关闭时自动完成.
用户控件构造函数的代码片段:
namespace GParts.UserControls
{
/// <summary>
/// Lógica de interacción para AnalogClock.xaml
/// </summary>
public partial class AnalogClock : UserControl
{
System.Timers.Timer timer = new System.Timers.Timer(1000);
public AnalogClock()
{
InitializeComponent();
MDCalendar mdCalendar = new MDCalendar();
DateTime date = DateTime.Now;
TimeZone time = TimeZone.CurrentTimeZone;
TimeSpan difference = …Run Code Online (Sandbox Code Playgroud) 我通过命令属性将命令绑定到按钮,并在放置的页面中执行命令绑定.在execute方法中,我创建一个包含后台worker的类的实例,然后启动它(这是一个很长的任务).后台worker(bw)类包含一个变量isRunning,它在执行DoWork方法之前设置为true,在执行RunWorkerCompleted时设置为false.因此,从放置按钮的页面后面的代码,在CanExecute方法中,如果bw没有运行(isRunning = false),则将e.canExecute设置为true,如果isRunning = true,则将e.canExecute设置为false.
当我按下按钮时,它会长时间启动bw进程并且该按钮被禁用.确定这是正确的,但是当后台工作人员(bw)完成时,按钮不会返回启用,直到我再次按下它.当它被禁用时我按下(当bw完成时)它被启用.为什么按钮没有自动返回到bw结束时启用?
我的代码片段:
<Page x:Class="GParts.Pages.MyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;
assembly=PresentationFramework.Aero"
xmlns:local="clr-namespace:GParts"
Loaded="Page_Loaded"
Unloaded="Page_Unloaded"
Height="Auto">
<Page.CommandBindings>
<CommandBinding Command="{x:Static local:Pages.MyPage.rcmd}"
Executed="CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute"/>
</Page.CommandBindings>
<...>
<Button Command="{x:Static local:Pages.MyPage.rcmd}" />
<...>
</Page>
Run Code Online (Sandbox Code Playgroud)
页面背后的代码:
namespace GParts.Pages
{
public partial class MyPage : Page
{
public static RoutedCommand rcmd = new RoutedCommand();
private cBgWorker bw;
<...>
// ExecutedRoutedEventHandler for the custom button remove all command.
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
// get an isntance of the background worker class
bw = …Run Code Online (Sandbox Code Playgroud) wpf ×4
animation ×1
button ×1
command ×1
performance ×1
progress-bar ×1
storyboard ×1
window ×1
xaml ×1