在 FM 收音机频率上前进和后退

Go forwards and backwards on FM radio frequency

我已经用xaml设计了一个后退按钮和一个前进按钮。当我单击后退按钮时,我的 FM 收音机实例应该开始根据信号强度寻找低于无线电频率的无线电频率。

嗨,有代码可以做到这一点

    private void FastForwardButton_Click(object sender, RoutedEventArgs e)
    { try
        { do
            {
                ManageFrequencyChange(0.1);
            }
            while (FMRadio.Instance.SignalStrength * 100 < 65);
        }
        catch (RadioDisabledException ex)
        {

        }
    }

以及这些的参考

    private void ManageFrequencyChange(double delta)
    {
        try
        {
            do
            {
                double frequency = FMRadio.Instance.Frequency; 
                frequency += delta;
                switch (wpradio.CurrentRegion)
                {
                    case RadioRegion.Europe:
                        if (frequency < minFrequencyEurope)
                            frequency = maxFrequencyEurope;
                        if (frequency > maxFrequencyEurope)
                            frequency = minFrequencyEurope;
                        break;
                    case RadioRegion.Japan:
                        if (frequency < minFrequencyJapan)
                            frequency = maxFrequencyJapan;
                        if (frequency > maxFrequencyJapan)
                            frequency = minFrequencyJapan;
                        break;
                    case RadioRegion.UnitedStates:
                        if (frequency < minFrequencyUnitedStates)
                            frequency = maxFrequencyUnitedStates;
                        if (frequency > maxFrequencyUnitedStates)
                            frequency = minFrequencyUnitedStates;
                        break;

                    default:
                        //Throw new ArgumentException();                             
                        break;
                }
            }
            while (!IsFrequencyCorrect(frequency));
            wpradio.Frequency = Math.Round(frequency, 1);
        }
        catch { }
    }


    private bool IsFrequencyCorrect(double frequency)
    {
        string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
        bool isCorrect = false;
        string frequencyAsString = Math.Round(frequency, 1).ToString();
        switch (wpradio.CurrentRegion)
        {
            case RadioRegion.Europe:
                isCorrect = frequencyAsString.EndsWith(string.Format("{0}1", separator)) || 
                    frequencyAsString.EndsWith(string.Format("{0}3", separator)) || 
                    frequencyAsString.EndsWith(string.Format("{0}5", separator)) || 
                    frequencyAsString.EndsWith(string.Format("{0}6", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}7", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}8", separator)) || 
                    frequencyAsString.EndsWith(string.Format("{0}0", separator));
                break;
            case RadioRegion.Japan:
                //check here the correctness of frequency for this position   
                isCorrect = frequencyAsString.EndsWith(string.Format("{0}1", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}2", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}3", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}5", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}6", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}7", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}8", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}9", separator)) ||
                    frequencyAsString.EndsWith(string.Format("{0}0", separator));
                break;
            case RadioRegion.UnitedStates:
                //check here the correctness of frequency for this position                     
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
        return 
            isCorrect;

并在初始化前定义最小和最大频率

    public partial class MainPage : PhoneApplicationPage
{              
    private const double minFrequencyEurope = 88;
    private const double maxFrequencyEurope = 108;
    private const double minFrequencyUnitedStates = 88.1;
    private const double maxFrequencyUnitedStates = 107.9;
    private const double minFrequencyJapan = 76;
    private const double maxFrequencyJapan = 90;
    public MainPage()