【发布时间】:2018-10-08 05:49:37
【问题描述】:
我正在尝试编写带有接口的依赖注入方法,以便我能够真正为 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();
}
}
这适用于 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 实现。那么,假设它是最佳选择,我应该如何修改它以“伪造”异步方法?或者有没有其他我没有想到的方法?
[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;
}
}
}
【问题讨论】:
标签: dependency-injection interface xamarin.android android-bluetooth xamarin.uwp