Windows应用商店应用程序节拍器
本文关键字:应用程序 节拍器 应用 Windows | 更新日期: 2023-09-27 18:29:47
我正在开发Windows应用商店应用程序。我需要用节拍器。这个节拍器应该有bpm设置。用户应该能够增加/减少它。
这是我到目前为止的代码:
namespace App1
{
public sealed partial class MainPage : Page
{
public class TickArgs : EventArgs
{
public DateTime Time { get; set; }
}
public class Metronome
{
public event TickHandler Tick = (m, e) => { };
public delegate void TickHandler(Metronome m, TickArgs e);
public void Start()
{
while (true)
{
System.Threading.Tasks.Task.Delay(3000);
Tick(this, new TickArgs { Time = DateTime.Now });
}
}
}
public class Listener
{
public void Subscribe(Metronome m, TextBlock tb, MediaElement mmx)
{
m.Tick += (mm, e) => mmx.Play();
}
}
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m, tbcheck, mediaElement1);
m.Start();
}
}
}
如何修改此代码以具有BPM设置?
我的问候
与其使用Task.Delay
,不如只使用Timer
您可以将BBM
传递到Start
方法中,并根据设置间隔
public class Metronome
{
private DispatcherTimer _timer;
public event TickHandler Tick;
public delegate void TickHandler(Metronome m, TickArgs e);
public Metronome()
{
_timer = new DispatcherTimer();
_timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Tick != null)
{
Tick(this, new TickArgs { Time = DateTime.Now });
}
}
public void Start(int bbm)
{
_timer.Stop();
_timer.Interval = TimeSpan.FromSeconds(60 / bbm);
_timer.Start();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m, tbcheck, mediaElement1);
m.Start(8); // 8bbm
}