0

The method I have been using to query an XElement works in Windows but not in Xamarin Android:

using System.Xml;
using System.Xml.Linq;
using System.Linq;

    public string ValidateLicense()
    {
        XElement xEl;
        string sMessage;
        Model.clsWebServiceFunctions cSvc = new Model.clsWebServiceFunctions();

        sMessage = cSvc.QueryWEB_ValidateLicense();
        xEl = cSvc.ServiceGetData(clsSettings.LicensingUrl, sMessage);

        var vResult = from results in xEl.DescendantsAndSelf("Result")
                      select results;

        return vResult.First().Element("LicensingResult").Value;
    }

The message I get is

Method 'First' not found in type'IEnumerable'1'

This seems to be due to Xamarin not supporting this method?!

I have do lots of similar queries in my app, so can someone recommend an alternative that will work in Xamarin? I could bodge this but don't know a good solution.

james pearce
  • 355
  • 1
  • 5
  • 20

1 Answers1

1

I'm having a similar issue on iOS.

This seems to be a problem with the Linq extension methods to IEnumerable not being loaded.

The error occurs within MvvmCross which is designed to work with the mobile platforms Xamarin supports. The issue doesn't occur initially when a fresh app is deployed, it takes some time to manifest, ie do a few days dev, attempt to run again on simulator.

Therefore I believe this is a linking problem with Xamarin. [Edit]: Not anymore, see below.

http://forums.xamarin.com/discussion/19483/pcl-linq-problem-on-ios-vs2013-sp2 - No answers here yet.

[Edit] OK So I was referencing a NET FX 3.5 DLL. changing the referenced assembly to NET 4.0 seemed to resolve the problem. Note that in the app output you can see this on startup. This is bad:

Loaded assembly: /Users/mike/Library/Caches/Xamarin/mtbs/builds/XamTestTouch2/7da682ee-1ab5-4b07-b745-c3c558a10329/assemblies/System.Core.dll [External]

This is good:

Loaded assembly: /Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll [External]

You may need to delete the Caches/Xamarin/mtbs/builds/AppName/GUID00-00000-0000.. folder after fixing your references to get things working again.

Michael Ribbons
  • 1,753
  • 1
  • 16
  • 26
  • I haven't tested this as I have moved away from Xamarin due to this kind of issue. Thanks for the response and I hope this will be of use to someone else. – james pearce Aug 06 '14 at 21:01
  • Thanks for the upvote. What are you using instead of Xamarin? I had to spend a few weeks getting things to work across all the platforms but I still think it's worth it. – Michael Ribbons Aug 08 '14 at 02:01
  • I moved to native code. I wanted to use Xamarin to reuse code but as I had to rewrite all of my data-parsing to get around the above issue, it didn't offer sufficient benefit for such a simple project, and if I'd got stuck on the next(much more complicated) project then I could have been in real trouble. At least with native code there's lots of help out there. – james pearce Aug 08 '14 at 09:57
  • I see where you're coming from. I played around for a while to work out the magic formula that works. The mono implementations seem to have everything required, it's just the compile time reference checks that mess everything up. This means a few interface classes here and there to get things working. Good luck James. – Michael Ribbons Aug 09 '14 at 01:04
  • @MichaelRibbons where do i set these assembly references? The reference to System.Core already exists in the iOS project. Code runs fine in Android but throws exception in iOS. – b.g Mar 08 '17 at 13:13
  • @b.g Are you referencing any .NET FX assemblies? I can't remember but it looks like the issue is caused by referencing non PCL assemblies. Are you referencing anything not provided by Microsoft or Xamarin? What does your Loaded assembly: System.Core.dll output look like? If it isn't the 2nd way, it means something is confusing xamarin so it is picking up the wrong version which doesn't contain the Linq methods. – Michael Ribbons Mar 09 '17 at 05:34