3
votes

I am new in C# and Xamarin I am creating a Xamarin Forms App and want o create an button that can handle different task from different platform (i.e. iOS & Android). Suppose I want to use a binding binding library (android binding Library and other is iOS Binding library). On a button click in Xamarin Forms App I want to execute platform specific library method for example suppose in Android Binding library there is a method void ShowAndroidContent(); and on the other iOS Binding library there is a method -(void)ShowiOSContent; so On the button click if device is Android ShowAndroidContent(); should be execute and if iOS then ShowiOSContent(); should be execute. My Solution Name is XamarinFormsAppDemo


TestXamarinFormAppPage.xaml Code is:-

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
  <StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
               AbsoluteLayout.LayoutFlags="All"
               BackgroundColor="Transparent">
            <Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
    </StackLayout>
</AbsoluteLayout>

TestXamarinFormAppPage.xaml.cs Code is:-

using System;
using Xamarin.Forms;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;

namespace TestXamarinFormApp
{
    public partial class TestXamarinFormAppPage : ContentPage
    {
        public TestXamarinFormAppPage ()
        {
            InitializeComponent ();
        }
        int count = 0;
        public void OnButtonClicked (object sender, EventArgs args)
        {




// here if device is Android the button click should be handle from MainActivity.cs in TestXamarinFormApp.Droid part and


// if device is an iOS then this button click should be handle from Main.cs in  TestXamarinFormApp.iOS




        }
    }
}

I'am using Xamarin Studio for it. Can anybody help me to do this. Please help me. Thank you.

3

3 Answers

4
votes

I got the solution of above problem It can be handle using DependencyService to achieve this goal I've created a .cs class for Interface object and using DependencyService we can handle click from platform specific class. the followings are the code to do it.

Interface_Object_Class.cs

using System;
using Xamarin.Forms;
namespace TestXamarinFormApp
{
    public interface IR_Btn_Click {
        void BtnPressed ();
    }
}

TestXamarinFormAppPage.xaml Code is:-

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestXamarinFormApp" x:Class="TestXamarinFormApp.TestXamarinFormAppPage">
<AbsoluteLayout>
  <StackLayout AbsoluteLayout.LayoutBounds="0,1,1,0.1"
               AbsoluteLayout.LayoutFlags="All"
               BackgroundColor="Transparent">
            <Button Text="Click Me !" Clicked="OnButtonClicked"></Button>
    </StackLayout>
</AbsoluteLayout>

TestXamarinFormAppPage.xaml.cs Code is:-

using System;
using Xamarin.Forms;
using System.Runtime.CompilerServices;
using System.ServiceModel.Channels;

namespace TestXamarinFormApp
{
    public partial class TestXamarinFormAppPage : ContentPage
    {
        public TestXamarinFormAppPage ()
        {
            InitializeComponent ();
        }
        public void OnButtonClicked (object sender, EventArgs args)
        {
            DependencyService.Get<IR_Btn_Click> ().BtnPressed ();
        }
    }

and now button click can be handle by registering assembly from MainPage.cs of iOS Part of app i.e. TestXamarinFormApp.iOS >>> MainPage.cs as follows:-

using System;
using AVFoundation;
using Xamarin.Forms;
using TestXamarinFormApp.iOS;
[assembly: Dependency (typeof (MainPage))]
namespace TestXamarinFormApp.iOS
{
    public class MainPage : IR_Btn_Click
    {
        public MainPage ()
        {
        }
        public void BtnPressed ()
        {
        Console.WriteLine ("Hurray ! the button is clicked from an iOS Device.");
        } 
    }
}
0
votes

use Device.OnPlatform()

public void OnButtonClicked (object sender, EventArgs args)
{

    Device.OnPlatform (iOS: () => { // do iOS stuff; },
                       Android: () => { // do Android stuff; });
}
0
votes

You can use the Device.OS. Device.OS will be set to one of the values of the TargetPlatform enumeration:

  • iOS
  • Android
  • WinPhone (returned for Windows 8 Silverlight)
  • Windows

This enables platform-specific checks to alter layout or functionality, such as these if statements:

public void OnButtonClicked (object sender, EventArgs args)
{
  if (Device.OS == TargetPlatform.iOS) 
  {
     //iOS
  } 
  else if (Device.OS == TargetPlatform.Android) 
  {
     //Android
  }
}