Go forwards and backwards on FM radio frequency

2019-06-11 16:15发布

问题:

I have already designed a back button and a forward button with xaml. When I click the back button, my FM radio instance should start looking for a radio frequency below the radio frequency according to signal strength.

回答1:

hi there is code to do that

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

        }
    }

and refrence for these

    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;

and define min and max frequency before initialize

    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()