Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Access Listview

I have a question regarding Xamarin forms in my shared library I have a Class called BluetoothPage.cs wich will compiled in Ios,Droid and WinPhone.

Now in Droid shared project I want to access the Listview from the BluetoothPage.cs which is in my shared Library (Portable)

I can make it static but thats not the way we want to go since I want to also bind data specific for every device.

Thats why I want to access it from my .Droid shared project and also from my .IOS shared project.

like image 482
Stefan van de Laarschot Avatar asked Nov 28 '25 11:11

Stefan van de Laarschot


1 Answers

Have a look at Custom renderers. What you need to do is write a custom renderer for bluetooth page and then you can access the instance of the forms page from all the platforms.

Shared Code

namespace BluetoothApp
{
    public ListView List {get; set;}

    public class  BluetoothPage : ContentPage
    {
    }

}

iOS

[assembly:ExportRenderer(typeof(BluetoothPage), typeof(BluetoothPageRenderer))]

namespace CustomRenderer.iOS
{
    public class BluetoothPageRenderer : PageRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<ContentPage> e)
        {
            base.OnElementChanged (e);

            var bluetoothPage = (BluetoothPage)e;

            bluetoothPage.List.DoStuff();
        }

    }

}

The Droid and Windows phone renderers will be slightly different but the idea is the same, you can find examples of custom renderers on the docs or here.

like image 105
koreus737 Avatar answered Nov 29 '25 23:11

koreus737