0

我正在开发 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 设置?

致以我的问候

4

1 回答 1

1

而不是 uisngTask.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
    }
于 2013-09-02T02:41:19.930 回答