【问题标题】:Xamarin Forms Access ListviewXamarin 表单访问列表视图
【发布时间】:2015-09-03 14:55:23
【问题描述】:

我有一个关于 Xamarin 表单的问题 在我的共享库中,我有一个名为 BluetoothPage.cs 的类 它将在 Ios、Droid 和 WinPhone 中编译。

现在在 Droid 共享项目中,我想从共享库(便携式)中的 BluetoothPage.cs 访问 Listview

我可以将其设为静态,但这不是我们想要的方式 因为我还想为每个设备绑定特定的数据。

这就是为什么我想从我的 .Droid 共享项目以及我的 .IOS 共享项目中访问它。

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

    看看Custom renderers。您需要做的是为蓝牙页面编写一个自定义渲染器,然后您可以从所有平台访问表单页面的实例。

    共享代码

    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();
            }
    
        }
    
    }
    

    Droid 和 Windows phone 渲染器会略有不同,但思路是一样的,您可以在文档或 here. 上找到自定义渲染器的示例

    【讨论】:

    • 嗯,看起来这对于一个简单的调用来说非常有用?我可以使用 Derectives #if__ANDROID__ 但我不能使用原生命名空间 Android.OS
    • 如果您想调用特定于设备的代码,您可以使用dependency service。这取决于。你在用这个列表做什么?
    猜你喜欢
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 2019-08-17
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2011-04-07
    相关资源
    最近更新 更多