适配器问题修改Xamarin.Forms BLE示例

时间:2015-08-18 11:06:34

标签: c# bluetooth xamarin

我使用的是Xamarin.Forms蓝牙HRM示例,here

我是Xamarin的新手并且使用C#编写应用程序。我有一些问题。首先,我想创建一个新的页面作为“主屏幕”,当应用程序连接到心率监视器时显示接收的特征。

我遇到的问题是,我还没有真正理解适配器是什么?因此,当我更改主PCL中return new NavigationPage (new DeviceList (Adapter));中的app.cs引用时,我遇到了通过适配器的问题。如何访问start_page(我的新页面)并能够在该页面中使用BLE功能,同时又能够从那里导航到DeviceList页面?

便携式类库

app.cs

namespace HeartRateMonitor
{
    public class App
    {
        static IAdapter Adapter;

        public static Page GetMainPage ()
        {   
            return new NavigationPage (new DeviceList (Adapter));
        }

        public static void SetAdapter (IAdapter adapter) {
            Adapter = adapter;
        }
    }
}

DeviceList

namespace HeartRateMonitor
{   
    public partial class DeviceList : ContentPage
    {   
        IAdapter adapter;
        ObservableCollection<IDevice> devices;

        public DeviceList (IAdapter adapter)
        {
            InitializeComponent ();
            this.adapter = adapter;
            this.devices = new ObservableCollection<IDevice> ();
            listView.ItemsSource = devices;

            adapter.DeviceDiscovered += (object sender,     DeviceDiscoveredEventArgs e) => {
                Device.BeginInvokeOnMainThread(() => {
                    devices.Add (e.Device);
                });
            };

            // Lots of functions to perform various BLE scanning operations, timeouts etc. 

        }).Start();
     }
}

由于

0 个答案:

没有答案
相关问题