简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM