Dependency injection 形成蓝牙接口选择

Dependency injection 形成蓝牙接口选择,dependency-injection,interface,xamarin.android,android-bluetooth,xamarin.uwp,Dependency Injection,Interface,Xamarin.android,Android Bluetooth,Xamarin.uwp,我正在尝试编写带有接口的依赖注入方法,以便能够为Android和UWP提供一个真正的用户界面 一次处理和测试一个功能。模式正在运行,但我的问题是,在UWP端,大多数函数是异步的,而它们不在Android上 所以我的问题是,我是否应该在android端“伪造”异步函数,如果是,如何 以下是我的例子: using System.Collections.Generic; using System.Threading.Tasks; namespace XamarinBTArduinoLed {

我正在尝试编写带有接口的依赖注入方法,以便能够为Android和UWP提供一个真正的用户界面

一次处理和测试一个功能。模式正在运行,但我的问题是,在UWP端,大多数函数是异步的,而它们不在Android上

所以我的问题是,我是否应该在android端“伪造”异步函数,如果是,如何

以下是我的例子:

using System.Collections.Generic;
using System.Threading.Tasks;

namespace XamarinBTArduinoLed
{
    public interface IBlueTooth
    {
        // as a first test, I will try to get a list of paired devices in both Android and UWP
        List<string> PairedDevices();
    }
}
使用System.Collections.Generic;
使用System.Threading.Tasks;
命名空间XamarinBtarudoid
{
公共接口IBlueTooth
{
//作为第一个测试,我将尝试获取Android和UWP中的配对设备列表
列出配对设备();
}
}
这适用于Android,但对于UWP,它需要

public interface IBlueTooth
    {
        // as a first test, I will try to get a list of paired devices in both Android and UWP
        Task<List<string>> PairedDevices();
    }
公共接口
{
//作为第一个测试,我将尝试获取Android和UWP中的配对设备列表
任务对设备();
}
这不适用于我当前的Android实现。那么,假设异步方法是最佳选择,我应该如何修改它以“伪造”异步方法呢?还是有其他我不想的方式

[assembly: Xamarin.Forms.Dependency(typeof(XamarinBTArduinoLed.Droid.BlueToothInterface))]
    namespace XamarinBTArduinoLed.Droid
    {

        public class BlueToothInterface : IBlueTooth
        {       
            public List<string> PairedDevices()
            {
                List<string> BTItems = new List<string>();

                BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
                if (adapter == null) throw new Exception("No BlueTooth Adapter Found.");
                if (!adapter.IsEnabled)
                {
                    adapter.Enable();
                }
                //if (!adapter.IsEnabled)
                //{
                //    throw new Exception("BlueTooth adapter is NOT enabled.");
                //}
                foreach (var item in adapter.BondedDevices)
                {     

                    BTItems.Add(item.Name + " - " + item.Type.ToString());
                }
                return BTItems;
            }
        }
    }
[程序集:Xamarin.Forms.Dependency(typeof(xamarinBTarduinold.Droid.BlueToothInterface))]
命名空间XamarinBtarudoinded.Droid
{
公共类Bluetooth接口:IBlueTooth
{       
公共列表PairedDevices()
{
List BTItems=新列表();
BluetoothAdapter=BluetoothAdapter.DefaultAdapter;
如果(adapter==null)抛出新异常(“未找到蓝牙适配器”);
如果(!adapter.IsEnabled)
{
adapter.Enable();
}
//如果(!adapter.IsEnabled)
//{
//抛出新异常(“蓝牙适配器未启用”);
//}
foreach(adapter.BondedDevices中的变量项)
{     
BTItems.Add(item.Name+“-”+item.Type.ToString());
}
归还物品;
}
}
}

我找到了最好的方法:

-1-我更改了接口以获取返回的任务:

public interface IBlueTooth
    {
        // as a first test, I will try to get a list of paired devices in both Android and UWP
        Task<List<string>> PairedDevices();
    }
公共接口
{
//作为第一个测试,我将尝试获取Android和UWP中的配对设备列表
任务对设备();
}
-2-我相应地修改了Android实现:

public Task<List<string>> PairedDevices()
        {
            List<string> BTItems = new List<string>();

            BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
            if (adapter == null) throw new Exception("No BlueTooth Adapter Found.");
            if (!adapter.IsEnabled)
            {
                adapter.Enable();
            }
            var t = Task.Factory.StartNew(() =>
                {
                    foreach (var item in adapter.BondedDevices)
                    {

                        BTItems.Add(item.Name);
                    }
                    return BTItems;
                });
            return t;
        }
public Task PairedDevices()
{
List BTItems=新列表();
BluetoothAdapter=BluetoothAdapter.DefaultAdapter;
如果(adapter==null)抛出新异常(“未找到蓝牙适配器”);
如果(!adapter.IsEnabled)
{
adapter.Enable();
}
var t=Task.Factory.StartNew(()=>
{
foreach(adapter.BondedDevices中的变量项)
{
BTItems.Add(item.Name);
}
归还物品;
});
返回t;
}
-3-我实现了inotifyproperty更改为在XAML中显示

现在它工作得很好,我从Android和Windows UWP中获得了我的USB/串行设备列表。为这两个平台创建整个流程肯定需要一段时间,但至少看起来我有一个良好的开端

如果您有任何意见、建议,请不要犹豫,也许有更好的方法