Can there be multiple instances of a dependency in Xamarin.Forms? -
i have mirrored code xlabs network state tracking on device here: xlabs inetwork android implementation
i've pretty aligned coding standards here , changed method names - working fine, except reason on android implementation unable event handlers respond, same way on ios.
this adaptation android:
[assembly: dependency(typeof(stm.droid.dependencyinjection.network))] [assembly: usespermission("android.permission.access_network_state")] [assembly: usespermission("android.permission.access_wifi_state")] [assembly: usespermission("android.permission.change_network_state")] [assembly: usespermission("android.permission.change_wifi_state")] namespace namespace.droid { [broadcastreceiver(enabled = true)] [intentfilter(new [] { android.net.connectivitymanager.connectivityaction })] public class network : broadcastreceiver, inetwork { /// <summary> /// internets connection status. /// </summary> /// <returns>networkstatus.</returns> public networkstatus getconnectionstatus() { var status = networkstatus.notreachable; using (var cm = (connectivitymanager)application.context.getsystemservice(context.connectivityservice)) using (var ni = cm.activenetworkinfo) { if (ni != null && ni.isconnectedorconnecting) { var name = ni.typename.toupper(); if (name.contains("wifi")) { status = networkstatus.reachableviawifinetwork; } else if (name.contains("mobile")) { status = networkstatus.reachableviacarrierdatanetwork; } else { status = networkstatus.reachableviaunknownnetwork; } } } return status; } private readonly object lockobject = new object(); private eventhandler<networkstatus> _reachabilitychanged; public event eventhandler<networkstatus> reachabilitychanged { add { lock (this.lockobject) { this._reachabilitychanged += value; } } remove { lock (this.lockobject) { this._reachabilitychanged -= value; } } } /// <summary> /// occurs when [reachability changed]. /// </summary> // public event eventhandler<networkstatus> reachabilitychanged; /// <summary> /// determines whether specified host reachable. /// </summary> /// <param name="host">the host.</param> /// <param name="timeout">the timeout.</param> public task<bool> isreachableasync(string host, timespan timeout) { return task.run( () => { try { var address = inetaddress.getbyname(host); return address != null; // && (address.isreachable((int)timeout.totalmilliseconds) || ); } catch (unknownhostexception) { return false; } }); } /// <summary> /// determines whether [is reachable wifi] [the specified host]. /// </summary> /// <param name="host">the host.</param> /// <param name="timeout">the timeout.</param> public async task<bool> isreachablebywifiasync(string host, timespan timeout) { return getconnectionstatus() == networkstatus.reachableviawifinetwork && await isreachableasync(host, timeout); } /// <summary> /// gets called os when <see cref="connectivitymanager.connectivityaction"/> <see cref="intent"/> fires. /// </summary> /// <param name="context">context intent.</param> /// <param name="intent">intent information.</param> public override void onreceive(context context, intent intent) { // workaround - oddly enough forwards events way expect to. messagingcenter.send(this inetwork, string.empty, getconnectionstatus()); // null! var handler = _reachabilitychanged; if (handler != null) { var connectionstatus = this.getconnectionstatus(); handler(this, connectionstatus); } } } }
the error in question occurs in onreceive.
while in debugging notice handlers attached correctly on pcl side - there null on _reachabilitychanged field oddly enough.
as debugging indicates apparently getting
{md50e2cce2f1202796e628729fe0540389b.network@b422684}
on droid onreceive method "this"
while on pcl side getting
{md50e2cce2f1202796e628729fe0540389b.network@b562641}
when calling dependencyservice.get
to me looks reason handler list empty, because subscribing handler on instance , therefore not handlers when different instance of network calls onreceive.
is possible xamarin.forms dependencyservice provide 2 or more instances of instance? because surprise me lot...
update:
in case needs workaround - works well:
public event eventhandler<networkstatus> reachabilitychanged { add { messagingcenter.subscribe(value.target, string.empty, (inetwork d, networkstatus a) => value(d, a)); } remove { messagingcenter.unsubscribe<inetwork>(value.target, string.empty); } }
Comments
Post a Comment