简体   繁体   中英

How to make Parse work with Xamarin's PCL project and MvvmCross?

I am having a lot of problems when trying to integrate Parse with cross-platform Xamarin app. At first, I was able to create Android app using Xamarin and MvvmCross and integrate with Parse service (done by adding Parse component in Xamarin). When it comes time to develop iOS app, I am having problem to refactor out Parse dependency into Core PCL project, since Parse currently doesn't support Xamarin PCL projects. Hence, there is no Parse NuGet package or Xamarin component that can be added to PCL project. I can easily add Parse component to iOS project and Android project respectively, but that will require lots of code duplication across both projects.

I've read that referencing Parse.dll and Parse.NetFx45.dll in Core project enables me to use Parse calls in Core project. I did this and are able to compile everything successfully. However, when I try to initialize Parse in Core project using ParseClient.Initialize("ApplicationID", "DotNetKey"); , I get TypeInitializationException during runtime.

Question is, what is the best way to integrate Parse service with Xamarin & MvvmCross cross-platform application? I'd imagine lots of people would have done this, but couldn't find references/examples. Duplicating codes across both iOS and Android project definitely shouldn't be the way to go.

Here are a couple of ideas:

First, if you are currently manually copying Parse code changes from project to project, you could speed up that process by writing a script that you run that would clone certain files to the other project. Or you could maybe write a grunt script that copies files automatically when it detects a change.

Now, here is another approach that stores Parse code in one project but is shared across platforms:

The Problem: I was using a Shared Project instead of a PCL to house my Xamarin.Forms and Parse code. The Parse code was working just fine, but I ran into an issue when trying to use my own custom ContentView in my ContentPage in XAML. I wanted a solution that would allow my custom ContentView to work as well as my Parse code -- both from the same project.

The Solution: I have switched to now housing my Xamarin.Forms and Parse code in a PCL. But there is a small catch. Before I explain the catch, just know that it does work with NO Parse code duplication (except for the one line of Parse initialization code that goes in each platform's specific project).

What's the catch? The catch is that in your portable class library you have to manually swap in and out either the Parse.iOS.dll or Parse.Android.dll depending on what you are compiling for at the time.

Does it increase the file size? No. I tested with my app using a Shared Project (where it uses the platform project's referene to the Parse dll) versus a Portable Class Library (where you have to add a reference to the dll in the platform's project as well as the PCL) and found no increase in the app file size by doing it this way.

Below is the project structure that is currently working for me (with project names renamed for confidentiality). (FYI: I am using Xamarin for Mac.)

MyProject.iOS
 - Reference to Parse.iOS.dll
 - Reference to the Portable Class Library
MyProject.Android
 - Reference to Parse.iOS.dll
 - Reference to the Portable Class Library
Portable Class Library
 - Parse Code
 - Xamarin Forms Code
 - Reference to either Parse.iOS.dll or Parse.Android.dll

Important : When swapping out the DLL in the PCL, I found that if I right-click the DLL under the References menu, and then click Delete, it caused issues in my project where the iOS project wouldn't compile anymore because it was still looking for the Parse.Android.dll or vice-versa. I tried cleaning the solution, deleting bin and obj folders from the solution's file system to no avail. I got it to work again by doing this: Right-click References, click Edit References, and then uncheck the one Parse DLL and check the box for the other. However, then after that, I tried the "Delete" method again and didn't have problems compiling. Who knows, maybe that issue will crop up for me again.

Won't this get annoying? Depending on how often you switch between platforms, the manual swapping of DLLs may or may not get annoying. No matter how annoying that is though, it can't be worse than having duplicate code. (Perhaps this swapping process could be automated with a script? I think you'd have to unload the PCL and reload it though if you modify the .csproj with a script. Anyone out there up for the challenge? Or perhaps there is a way to do a conditional reference to the DLL based on the platform it is compiling for. Thoughts anyone?)

I hope this helps. If you like any of these ideas better than what you are currently doing please accept this as the answer and let us know which route you decide to take.

Isn't the best way to to this, to create a Parse Plugin for Mvvmcross? Where you define your interfaces that want to use.

And in each platform specific library you reference the parse dll and call the method. So in you Core project you should be able to call the IParse.methods...

So I got it working fine on both Android and iOS by referencing Parse.Android.dll in all 3 projects (Core, iOS, and Android). Yes it sounds weird, but that's what's working for me. Just go to Parse download page and download Xamarin SDK under Android section. It works wonders too! No code duplications, no mess :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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