简体   繁体   English

NetworkChange.NetworkAddressChanged事件不会触发

[英]NetworkChange.NetworkAddressChanged event does not fire

I am developing a windows phone 7 app which is required to work with network. 我正在开发Windows Phone 7应用,该应用需要与网络配合使用。

I wanted my application to connect when NetworkAddress is changed that it becomes available. 我希望我的应用程序在NetworkAddress更改为可用时能够连接。 So I used NetworkChange.Networkaddresschanged. 因此,我使用了NetworkChange.Networkaddresschanged。 I was testing my app on emulator. 我正在模拟器上测试我的应用程序。 It fires up first time but as there is no network I do nothing. 它第一次启动,但是由于没有网络,我什么也没做。 This NetworkAddresschanged does not fire up the second time when network is available. 当网络可用时,不会第二次启动此NetworkAddresschanged。

My code is 我的代码是

    public void OnNetworkDownEvent()
    {
        lock (_networkChange)
        {
            var handler =_OnNetworkDown;
            if (handler != null)
            {
                _OnNetworkDown();
            }
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                NetworkChange.NetworkAddressChanged -= OnNetworkChange;
                NetworkChange.NetworkAddressChanged += OnNetworkChange;
            });


            _connectionDown = true;

            Monitor.Wait(_networkChange);
            OnNetworkUpEvent();
        }
    }


    public void OnNetworkUpEvent()
    {
        var handler = _OnNetworkUp;
        if (handler != null)
        {
            _OnNetworkUp();



        }
    }

    private void OnNetworkChange(object sender, EventArgs e)
    {
        lock(_networkChange)
        {
            if(NetworkInterface.GetIsNetworkAvailable())
            {

                if (_connectionDown)
                {
                    _connectionDown = false;
                    Monitor.Pulse(_networkChange);
                    //OnNetworkUpEvent();
                }
            }
        }
    }

I call Networkdownevent() when network is down.And at that time I attach a delegate to NetworkAddressChange. 当网络中断时,我调用Networkdownevent() 。那时我将委托附加到NetworkAddressChange。

I do not know why this is happening. 我不知道为什么会这样。

The phone will see the USB connection to the host PC as it's primary connection so changes to the network connection of the attached PC will not have an impact on the phone. 手机将看到与主机PC的USB连接,这是它的主要连接,因此更改连接PC的网络连接不会对手机造成影响。

This is one scenario where you can't test with the debugger attached. 这是您无法使用调试器进行测试的一种情况。 You'll have to store/display your debug output on the device with it not attached. 您必须在未连接的设备上存储/显示调试输出。
For testing I recommend having the phone connect to WiFi only and then controlling the connection state by turning the access point on or off. 为了进行测试,我建议您仅将手机连接到WiFi,然后通过打开或关闭接入点来控制连接状态。 (This is the simplest technique I'm aware of for such a situation.) (这是我所知道的最简单的技术。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM