当前位置: 动力学知识库 > 问答 > 编程问答 >

How to implement a timer which calls a button's event in WPF C#?

问题描述:

As I understand, the DispatcherTimer's Tick event needs to be called with EventArgs, whereas the same event calls the action of some buttons, which need to be called with RoutedEventArgs. Obviously, I cannot call the buttons' events directly. My question is: is there any way to implement this, by all means?

private void song_timer_Tick(object sender, EventArgs e)

{

if (playing == true)

{

lbl_curent_time.Text = audio_device.audioFileReader.CurrentTime.ToString();

if (music_state_progress.Value < music_state_progress.Maximum)

{

music_state_progress.Value = music_state_progress.Value + 1;

taskbar.SetProgressValue(Convert.ToInt32(music_state_progress.Value), Convert.ToInt32(music_state_progress.Maximum));

}

if (lbl_curent_time.Text == audio_device.audioFileReader.TotalTime.ToString())

if (music_list.SelectedIndex < music_list.Items.Count - 1)

{

btn_next_Click(sender, e);

music_list_DoubleClick(sender, e);

}

else if (repeat_checkbox.IsChecked == true)

{

music_list.SelectedIndex = 0;

music_list_DoubleClick(sender, e);

}

else

{

btn_stop_Click(sender, e);

lbl_curent_time.Text = "00:00:00.0000000";

lbl_max_time.Text = "00:00:00.0000000";

}

}

}

网友答案:

Replace:

btn_stop_Click(sender, e);

with:

btn_stop.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));
网友答案:
ButtonAutomationPeer peer =
  new ButtonAutomationPeer( someButton );
IInvokeProvider invokeProv =
  peer.GetPattern( PatternInterface.Invoke )
  as IInvokeProvider;
invokeProv.Invoke();

Taken from HERE

分享给朋友:
您可能感兴趣的文章:
随机阅读: