C# 使用MVVMCross在何处收听广播操作
制作蓝牙应用程序时,我需要访问代码Android端的一些广播操作 所有这些都在我的内核中运行,因此我有一个ViewModel,它将通过我的接口调用C# 使用MVVMCross在何处收听广播操作,c#,mono,xamarin.android,mvvmcross,C#,Mono,Xamarin.android,Mvvmcross,制作蓝牙应用程序时,我需要访问代码Android端的一些广播操作 所有这些都在我的内核中运行,因此我有一个ViewModel,它将通过我的接口调用 public interface IConnectionService { //Properties string IntentName { get; } //Events event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundE
public interface IConnectionService
{
//Properties
string IntentName { get; }
//Events
event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
//Methods
void RunIntent();
void SearchConnection();
void Connect(string macAddress);
}
using System;
using Android.Bluetooth;
using Android.Content;
using Cirrious.MvvmCross.Android.Platform.Tasks;
using Test.Core.Interfaces;
using Test.Core.Models;
namespace Test.Core.Android
{
public class AndroidBluetooth : MvxAndroidTask, IConnectionService
{
#region Private Fields
private readonly BluetoothAdapter _bluetoothAdapter;
#endregion
#region Public Fields
#endregion
#region Properties
public string IntentName { get { return "Turn on Bluetooth"; }}
#endregion
#region Constructor
public AndroidBluetooth()
{
_bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
}
#endregion
#region Events
public event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
private void ItemFound(SearchConnectionItemEventArgs e)
{
if(SearchItemFoundEvent != null)
{
SearchItemFoundEvent(this, e);
}
}
#endregion
#region Methods
public void RunIntent()
{
if (_bluetoothAdapter == null)
{
//No bluetooth support on phone
}
else if(!_bluetoothAdapter.IsEnabled)
{
var intent = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivity(intent);
}
}
public void SearchConnection()
{
if (_bluetoothAdapter == null)
{
//No bluetooth support on phone
}
else if (!_bluetoothAdapter.IsEnabled)
{
//Bluetooth not turned on
RunIntent();
}
else
{
FindBondedDevices();
}
}
private void FindBondedDevices()
{
var pairedDevices = _bluetoothAdapter.BondedDevices;
if (pairedDevices.Count <= 0) return;
foreach (var item in pairedDevices)
{
ItemFound(new SearchConnectionItemEventArgs {Name = item.Name, MacAddress = item.Address});
}
}
private void FindNewDevices()
{
if (_bluetoothAdapter == null)
{
}
else if (!_bluetoothAdapter.IsEnabled)
{
}
else
{
_bluetoothAdapter.StartDiscovery();
//Bind event for new devices
}
}
public void Connect(string macAddress)
{
}
#endregion
}
}
当我搜索我需要的新设备时
Android.Bluetooth.BluetoothDevice.ActionFound
但是我不能将[Android.Runtime.Register(“ACTION_FOUND”)]和[Android.Runtime.Register(“ACTION_STATE_CHANGED”)]放在我的类上,这只在我尝试将其放在视图上时有效,但如果我这样做,我的视图中需要逻辑
有没有可能把它放在核心里
我的类实现了我的接口
public interface IConnectionService
{
//Properties
string IntentName { get; }
//Events
event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
//Methods
void RunIntent();
void SearchConnection();
void Connect(string macAddress);
}
using System;
using Android.Bluetooth;
using Android.Content;
using Cirrious.MvvmCross.Android.Platform.Tasks;
using Test.Core.Interfaces;
using Test.Core.Models;
namespace Test.Core.Android
{
public class AndroidBluetooth : MvxAndroidTask, IConnectionService
{
#region Private Fields
private readonly BluetoothAdapter _bluetoothAdapter;
#endregion
#region Public Fields
#endregion
#region Properties
public string IntentName { get { return "Turn on Bluetooth"; }}
#endregion
#region Constructor
public AndroidBluetooth()
{
_bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
}
#endregion
#region Events
public event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
private void ItemFound(SearchConnectionItemEventArgs e)
{
if(SearchItemFoundEvent != null)
{
SearchItemFoundEvent(this, e);
}
}
#endregion
#region Methods
public void RunIntent()
{
if (_bluetoothAdapter == null)
{
//No bluetooth support on phone
}
else if(!_bluetoothAdapter.IsEnabled)
{
var intent = new Intent(BluetoothAdapter.ActionRequestEnable);
StartActivity(intent);
}
}
public void SearchConnection()
{
if (_bluetoothAdapter == null)
{
//No bluetooth support on phone
}
else if (!_bluetoothAdapter.IsEnabled)
{
//Bluetooth not turned on
RunIntent();
}
else
{
FindBondedDevices();
}
}
private void FindBondedDevices()
{
var pairedDevices = _bluetoothAdapter.BondedDevices;
if (pairedDevices.Count <= 0) return;
foreach (var item in pairedDevices)
{
ItemFound(new SearchConnectionItemEventArgs {Name = item.Name, MacAddress = item.Address});
}
}
private void FindNewDevices()
{
if (_bluetoothAdapter == null)
{
}
else if (!_bluetoothAdapter.IsEnabled)
{
}
else
{
_bluetoothAdapter.StartDiscovery();
//Bind event for new devices
}
}
public void Connect(string macAddress)
{
}
#endregion
}
}
使用系统;
使用Android.Bluetooth;
使用Android.Content;
使用cirries.MvvmCross.Android.Platform.Tasks;
使用Test.Core.interface;
使用Test.Core.Models;
名称空间Test.Core.Android
{
公共类AndroidBluetooth:MvxAndroidTask,IConnectionService
{
#区域专用字段
专用只读BluetoothAdapter\u BluetoothAdapter;
#端区
#区域公共领域
#端区
#区域属性
公共字符串IntentName{get{return“打开蓝牙”;}
#端区
#区域构造函数
公共AndroidBluetooth()
{
_bluetoothAdapter=bluetoothAdapter.DefaultAdapter;
}
#端区
#地区活动
公共事件事件处理程序SearchItemFoundEvent;
找到私有无效项(SearchConnectionItemMeventargs e)
{
if(SearchItemFoundEvent!=null)
{
SearchItemFoundEvent(本,e);
}
}
#端区
#区域方法
public void RunIntent()
{
如果(_bluetoothAdapter==null)
{
//手机上不支持蓝牙
}
否则如果(!\u bluetoothAdapter.IsEnabled)
{
var intent=新的intent(BluetoothAdapter.ActionRequestEnable);
星触觉(意向);
}
}
公共连接()
{
如果(_bluetoothAdapter==null)
{
//手机上不支持蓝牙
}
否则如果(!\u bluetoothAdapter.IsEnabled)
{
//蓝牙未打开
RunIntent();
}
其他的
{
findbondevices();
}
}
私有void findbondevices()
{
var pairedDevices=_bluetoothAdapter.BondedDevices;
if(pairedDevices.Count免责声明:我不熟悉Android的这一部分-我从未使用过蓝牙协议栈
从您的描述中,听起来您已经知道了答案-这些属性需要在活动/视图中的方法上继续
当然,一旦它们被添加到活动/视图中,就很容易将这些方法调用路由到ViewModel-只需在视图中使用ViewModel
属性即可
首先尝试将这部分作为一个独立的示例来使用,然后找出如何使其跨平台和/或mvvm,可能会更容易一些。免责声明:我不熟悉Android的这部分-我从未使用过蓝牙堆栈
从您的描述中,听起来您已经知道了答案-这些属性需要在活动/视图中的方法上继续
当然,一旦它们被添加到活动/视图中,就很容易将这些方法调用路由到ViewModel-只需在视图中使用ViewModel
属性即可
首先尝试将这一部分作为一个独立的示例来使用可能更容易,然后解决如何使其跨平台和/或mvvm。我只想将其隐藏在界面后面,如果我必须在视图上实现它,我需要为我想要的每种设备类型提供一个实现(如果您还记得,我想支持“any”类型)所以我需要在视图上为所有类型的设备编写特殊操作广播接收器,即使一次只需要一个,如果可能的话,我可能会考虑注入一个视图。我只想把它隐藏在接口后面,如果我必须在视图上实现它,我需要为我想要的每种设备类型实现它(如果您还记得,我想支持“any”类型)因此我需要为视图上的所有类型的设备编写特殊操作广播接收器,即使一次只需要一个,如果可能的话,可能会考虑注入视图