在FM无线电频率上前进和后退

时间:2016-10-14 06:38:59

标签: c# windows-phone-8 windows-phone-8.1 uwp windows-phone-silverlight

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

1 个答案:

答案 0 :(得分:0)

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

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