0

The only platform-specific code in my app comes from native libraries. So I see two options:

1. iOS and Android projects referencing their appropriate bindings. This would require both of them to handle UI events that require these platform-dependent libraries.

2. Create a Shared Project that would create wrappers around binding projects to create a uniform API and then use compiler directives to choose appropriate implementation.

I'm uncertain if the first option is achievable: handling UI events natively and returning shared response back to the cross-platform xamarin forms project for further processing.

I believe in the second option, both apps would contain ALL code. I also do not know if I can use a Shared Project if I already use .NET Standard code sharing strategy(required as .NET standard does not support compiler directives).

Justas S
  • 584
  • 4
  • 12
  • 34

1 Answers1

1

I'm uncertain if the first option is achievable: handling UI events natively and returning shared response back to the cross-platform xamarin forms project for further processing.

It is achievable, as all the forms' Controls are rendered into native through renderers. You can only access the native control through Custom Renderer. And for returning shared response back to the forms project, you can achieve that through Custom Events on Custom controls just like Cusom ListView Sample's ItemSelected Event:

In PCL:

public class NativeListView : ListView
{
  ...
  public event EventHandler<SelectedItemChangedEventArgs> ItemSelected;
  ...
}

In Custom Renderer:

Control.ItemClick += OnItemClick;

...
void OnItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
    ((NativeListView)Element).NotifyItemSelected(((NativeListView)Element).Items.ToList()[e.Position - 1]);
}

But if you have lots of plaform-specific functionality in your project. Using Shared Project is a good option.

I also do not know if I can use a Shared Project if I already use .NET Standard code sharing strategy(required as .NET standard does not support compiler directives).

I believe what you need is to change your PCL project into a Shared project. Of course some codes modifications are necessary. But that won't be much. If you mean you want your PCL to reference a new Shared Project. No, I don't think it will work.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24