NEW SAMPLE: CrossPlatformEssentials (File location, OpenGL texture load, Gestures)

SteveShaw.5557SteveShaw.5557 USMember ✭✭✭

Xamarin.Forms raises the tantalizing possibility of having ALL one's code cross-platform, except for a few bits that handle platform-specific details.

The existing examples don't attempt to handle some fundamental aspects of app programming.
This is an unnecessary limitation, for functionality that exists in some form on all the platforms that you are targeting.

Attached find "CrossPlatformEssentials.rar"; an example that targets iOS and Android.
It uses a partial class between a shared project and the platform-specific projects, to implement some cross-platform features that are currently "incomplete" in Xamarin.Forms.


*** IMPORTANT LIMITATIONS ***
A. I did not take the time to write cross-platform gesture code. This uses Mr.Gestures, which is 10 euro license per app name.
Gestures only work because I've altered the app name to be "GestureSample",
and used the license key from the downloaded GestureSample app.
To make this code work in YOUR app, you will need to purchase Mr.Gestures, giving your app's name,
and paste the corresponding license key in Android / MainActivity.cs and iOS / AppDelegate.cs,
where you see a line similar to "MR.Gestures.Android.Settings.LicenseKey = ...".
NOTE: The variable path on iOS has ".iOS." rather than ".Android.".

B. OpenGlView in Xamarin.Forms 1.4.2.6359 is broken on Android.
Unless you are trying this on a newer revision that fixes that,
you must go back to an older version:
"Add packages" "xamarin.forms version:1.4",
then select 1.4.0.6341
Or use any 1.3.x version.


"XU" (cross-platform Utilities) is the partial class, that contains some platform-specific methods. ALL the rest of the code is cross-platform (except of course for app startup). You write your app in the shared project, extending XU as needed.

  1. File location
    BaseFolder: this path represents a place you can read and write files.

    NOTE: Android seems to have more accessible locations than iOS. (Or maybe I just don't know iOS well enough).
    GetSharedDataFolder and GetAppDataFolder are two additional Android folders.

  2. Resources.
    StreamResource: calls GetManifestResourceStream. This works on both platforms, for resources that you mark Build Action as "EmbeddedResource".
    I also explicitly set "Resource Id" (which is a string, not an integer) in the Properties for each resource file, so that I could organize the files into folders without requiring a lengthier path to access them.

    NOTE: Android resources - in XU.Android.cs there is logic for accessing "Android resources" by their integer id. E.g. "drawable"s at different resolutions.
    I don't know if there is an iOS equivalent.

  3. OpenGL ES 2.0
    "LoadBitmapDataFromStream" - Once you have an image file or resource read into a stream, this gets it into a platform-specific bitmap, and then uses OpenGL TexImage2D to load that into the current texture.
    You'll see by my comments that I don't know OpenGL well enough to understand how it knows where it is putting that texture.
    Based on code from TexturedCube, adapted to work in OpenGLView.

  4. Gestures.
    Uses Mr.Gestures.

I'm not asserting any copyright on any of this. IMHO, This is how Xamarin.Forms should work, and I hope this accelerates adoption of fully cross-platform classes.

~ Steve Shaw aka ToolmakerSteve

Posts

Sign In or Register to comment.