A Technique for Sharing UI code without using Xamarin Forms

Port25Port25 ✭✭USMember ✭✭
edited November 2017 in Cross Platform with Xamarin

Hello,

I would like your advice on a technique that I am in the process of using for cross-platform native development:

1) I moved all of my code (including platform specific UI code) to the main "App" directory
2) I am not using storyboarding for either platform (layout/constraints done via code)
3) I preface all of my shared code like this:

//preface start 

using System;
using System.Globalization;

#if __IOS__
using UIKit;
namespace App.iOS
#endif

#if __DROID__
using App.Droid.iOS;
namespace App.Droid
#endif

//preface end 

//shared code begins here (using iOS APIs only)
{
    public partial class MyFoo : UIView
    {
          public void Foo()
          {
              var cgSize = new CGSize(0, 0); //example
              var guid = GetGuid();
          }
    }
}

4) I write for the iOS platform. I do not use any Android specific APIs in "App", only iOS
5) I then proceed to write wrappers in "App.Droid" for the iOS API, which looks something like this:

using System;
namespace App.Droid.iOS
{
    public struct CGSize
    {
        public float Width;
        public float Height;

        public CGSize(float width, float height)
        {
            Width = width;
            Height = height;
        }
    }
}

6) Any bits of code in the iOS or Droid API which I cannot easily duplicate 1 for 1, I write in the platform specific directory as a partial (the partial they share is shown in #1). The following isn't the best example (could create a NSUuid wrapper like in #5), but you get the idea:

using System;
using Java.Util;

namespace App.Droid
{
    public partial class MyFoo : UIView
    {
        public Guid GetGuid()
        {
            return new Guid(UUID.RandomUUID().ToString());
        }
    }
}

and this:

using System;
using Foundation;

namespace App.iOS
{
    public partial class MyFoo : UIView
    {
        public Guid GetGuid()
        {
            return new Guid(new NSUuid().ToString());
        }
    }
}

So far things are looking good; however, I am having difficulty with my understanding of the Android pixel/dp setup. The API appears to use both units, and it isn't clear to me when I should be setting dp units vs pixels. iOS is always points. This doesn't appear to be the case with Android.. It appears Xamarin is a mishmash of pixels and dp units, and I'm not sure what property uses what unit...I hope I'm missing something, but here is an example:

//LayoutParamaters is using pixels?
SetContentView(view, new AbsoluteLayout.LayoutParams(displayMetrics.WidthPixels, displayMetrics.HeightPixels, 0, 0)); 

//text size is using dp/sp?
paint.TextSize

Anyway, I would rather do things this way, than use Xamarin Forms, or duplicating all my UI code--which is extensive. So, any tips on how to improve this technique, comments, or suggestions would be great.

Posts

Sign In or Register to comment.