我有WP7和Android的应用程序,此应用程序必须支持“任何”连接类型(WiFi,NFC,蓝牙等)
然后我创建了一个MVVMCross https://github.com/slodge/MvvmCross
的分层模型我有一个界面,例如Android蓝牙必须实现
interface IConnectionService
{
List<TargetDevice> FindDevices();
void Connect(TargetDevice targetDevice);
void Disconnect();
byte[] Read();
void Write(byte[] command);
}
我希望能够请求用户进行蓝牙访问,但我不想将我的UI专门编程到Android蓝牙,因此视图和视图模型不应该知道使用了哪个意图,所有这些都应该被处理由实现IConnectionService的类
问题是它应该适用于不使用意图的Windows Phone,它使用任务,那么如何创建一个允许我发出Intent请求或任务请求的接口,而不知道什么类型的请求是否需要?
答案 0 :(得分:5)
这类似于MvvmCross允许用户拨打电话的方式。
使用此模式时:
ViewModel代码通过接口使用与平台无关的服务 - 例如:
public interface IMvxPhoneCallTask
{
void MakePhoneCall(string name, string number);
}
消耗的
protected void MakePhoneCall(string name, string number)
{
var task = this.GetService<IMvxPhoneCallTask>();
task.MakePhoneCall(name, number);
}
https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/BaseViewModel.cs 中的
应用设置代码为接口注入特定于平台的实现 - 例如:
RegisterServiceType<IMvxPhoneCallTask, MvxPhoneCallTask>();
在WP7中 - 它使用PhoneCallTask
- https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/WindowsPhone/Platform/Tasks/MvxPhoneCallTask.cs
public class MvxPhoneCallTask : MvxWindowsPhoneTask, IMvxPhoneCallTask
{
#region IMvxPhoneCallTask Members
public void MakePhoneCall(string name, string number)
{
var pct = new PhoneCallTask {DisplayName = name, PhoneNumber = number};
DoWithInvalidOperationProtection(pct.Show);
}
#endregion
}
在Droid中 - 它使用ActionDial Intent
- https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Android/Platform/Tasks/MvxPhoneCallTask.cs
public class MvxPhoneCallTask : MvxAndroidTask, IMvxPhoneCallTask
{
#region IMvxPhoneCallTask Members
public void MakePhoneCall(string name, string number)
{
var phoneNumber = PhoneNumberUtils.FormatNumber(number);
var newIntent = new Intent(Intent.ActionDial, Uri.Parse("tel:" + phoneNumber));
StartActivity(newIntent);
}
#endregion
}
触控 - 它只使用网址 - https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Touch/Platform/Tasks/MvxPhoneCallTask.cs
public class MvxPhoneCallTask : MvxTouchTask, IMvxPhoneCallTask
{
#region IMvxPhoneCallTask Members
public void MakePhoneCall(string name, string number)
{
var url = new NSUrl("tel:" + number);
DoUrlOpen(url);
}
#endregion
}
在mvvmcross的vnext版本中,这种方法使用插件进一步形式化 - 请参阅http://slodge.blogspot.co.uk/2012/06/mvvm-mvvmcross-monodroid-monotouch-wp7.html中的“便携式”演示文稿中的简要介绍
例如,在vNext中,上面的电话代码包含在PhoneCall插件中 - https://github.com/slodge/MvvmCross/tree/vnext/Cirrious/Plugins/PhoneCall
您的任务的挑战之一可能是“任何”一词 - 平台实施的差异可能使得难以定义适用于NFC,蓝牙等任何一个平台的所有平台的跨平台界面,让他们都是。