为什么我更改时无线无线电状态会刷新?

时间:2012-12-06 22:24:58

标签: c# windows-mobile opennetcf

我正在尝试创建一个简单的应用程序来切换Windows移动设备上的WiFi无线电,并发现OpenNETCF.WIndowsMo​​bile命名空间提供了一些宝石,使这项任务变得非常简单:

    private void button2_Click(object sender, EventArgs e)
    {
         var wifiRadio = (from radio in Radios.GetRadios()
               where radio.RadioType == RadioType.WiFi
               select radio).FirstOrDefault();

        if (wifiRadio != null)
            switch (wifiRadio.RadioState)
            {
                case RadioState.Off:
                    wifiRadio.RadioState = RadioState.On;
                    button2.Text = "Is On";
                    break;
                case RadioState.On:
                    wifiRadio.RadioState = RadioState.Off;
                    button2.Text = "Is Off";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }                            
    }

第一次点击时效果很好。在所有后续点击中,RadioState仍然报告相同(即使已切换无线电)。

重新启动应用程序后,RadioState将再次返回正确的状态。

有没有办法刷新状态,以便正确报告?

1 个答案:

答案 0 :(得分:2)

我发现有一种刷新方法。如果其他人需要知道如何利用它:

        private void button2_Click(object sender, EventArgs e)
    {
        var radios = Radios.GetRadios();
        radios.Refresh();

        var wifiRadio = (from radio in radios
               where radio.RadioType == RadioType.WiFi
               select radio).FirstOrDefault();

        if (wifiRadio != null)
            switch (wifiRadio.RadioState)
            {
                case RadioState.Off:
                    wifiRadio.RadioState = RadioState.On;
                    button2.Text = "Is On";
                    break;
                case RadioState.On:
                    wifiRadio.RadioState = RadioState.Off;
                    button2.Text = "Is Off";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }                            
    }
}