Xamarin Android - 排序iBeacon iCollection导致异常

时间:2014-10-06 13:43:05

标签: android sorting xamarin ibeacon icollection

我正在尝试对iBeacon对象的iCollection进行排序,但是一旦代码运行就会抛出异常。我试图运行的代码如下。它工作正常,没有任何排序或排序,但我需要能够在接近时对集合进行排序,如图所示(最低接近度,即接近度等于1):

async void RangingBeaconsInRegion(object sender, RangeEventArgs e)
   {

     if (e.Beacons.Count > 0) 
      {
        var orderedBeacons = e.Beacons.OrderBy(b => b.Proximity); //this causes the problem

      foreach (var beacon1 in orderedBeacons) 
       {
          //do all my stuff here on my ordered beacons
       }
    }
}

以下是例外:

[mono] Unhandled Exception:
[mono] System.ArgumentException: 'jmethod' must not be IntPtr.Zero.
[mono] Parameter name: jmethod
[mono]   at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00000] in <filename unknown>:0 
[mono]   at Android.Runtime.JavaCollection`1[RadiusNetworks.IBeaconAndroid.IBeacon].CopyTo (RadiusNetworks.IBeaconAndroid.IBeacon[] array, Int32 array_index) [0x00000] in <filename unknown>:0 
[mono]   at System.Linq.Enumerable.ToArray[IBeacon] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
[mono]   at System.Linq.QuickSort`1[RadiusNetworks.IBeaconAndroid.IBeacon]..ctor (IEnumerable`1 source, System.Linq.SortContext`1 context) [0x00000] in <filename unknown>:0 
[mono]   at System.Linq.QuickSort`1+<Sort>c__Iterator0[RadiusNetworks.IBeaconAndroid.IBeacon].MoveNext () [0x00000] in <filename unknown>:0 

我有什么想法可以正确订购这个系列吗?

1 个答案:

答案 0 :(得分:1)

这对你有用。让我知道它是怎么回事。

var allBeacons = new List<Beacon>();

foreach(var b in e.Beacons)
{
    allBeacons.Add(b);
}

var orderedBeacons = allBeacons.OrderBy(b => b.Proximity);