为任务/意图提供单跨平台支持 [英] Making mono cross platform support for Task/Intent

查看:53
本文介绍了为任务/意图提供单跨平台支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于WP7和Android的应用程序,并且该应用程序必须具有对任何"连接类型(WiFi,NFC,蓝牙等)的支持.

I have a application for WP7 and Android, and this application must have supporthas support for "any" connection type (WiFi, NFC, Bluetooth etc)

然后,我使用MVVMCross https://github.com/slodge/MvvmCross 创建了分层模型

I have then created a layered model with MVVMCross https://github.com/slodge/MvvmCross

我有一个接口,例如Android蓝牙必须实现

I have an interface for example Android Bluetooth must implement

interface IConnectionService
{
    List<TargetDevice> FindDevices();
    void Connect(TargetDevice targetDevice);
    void Disconnect();
    byte[] Read();
    void Write(byte[] command);
}

我希望能够请求用户进行蓝牙访问,但是我不想为UI专门针对Android蓝牙编程,因此视图和视图模型不应该知道使用了哪种意图,所有这些都应该处理通过实现IConnectionService的类

I want to be able to request the user for Bluetooth Access, but I do not want to program my UI specifically to Android Bluetooth, so the view and view-model should not know which intent is used, all this should be handled by the class implementing IConnectionService

问题在于它也可以在不使用意图的Windows Phone上使用,它使用任务,因此如何创建一个界面,使我可以发出意图请求或任务请求,而无需任何人知道哪种类型的需要请求吗?

The issue is that it should also work for Windows Phone which do not use intents, it uses tasks, so how do I make an interface that allows me to make either a Intent request or a task request without anyone knowing what type of request is needed?

推荐答案

这类似于MvvmCross允许用户拨打电话的方式.

This is similar to the way MvvmCross allows users to make phone calls.

使用此模式时:

ViewModel代码通过接口使用与平台无关的服务-例如:

The ViewModel code consumes a platform independent service via an interface - e.g.:

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

应用设置代码为界面注入平台特定的实现-例如:

The app setup code injects the platform specific implementation for the interface - e.g:

        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

In Droid - it uses the 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
}

接触-它仅使用Urls- https://github.com/slodge/MvvmCross/blob/master/Cirrious/Cirrious.MvvmCross/Touch/Platform/Tasks/MvxPhoneCallTask​​.cs

In Touch - it just uses Urls - 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版本中,使用插件进一步规范了此方法-请参见例如,在vNext中,上面的电话代码包含在PhoneCall插件中-

For example, in vNext the phone call code above is contained in the PhoneCall plugin - https://github.com/slodge/MvvmCross/tree/vnext/Cirrious/Plugins/PhoneCall

您的任务面临的挑战之一可能是任何"一词-平台实施上的差异可能使定义跨平台接口(适用于NFC,蓝牙等任何平台的跨平台接口)变得困难,让独自一人.

One of the challenges of your task may be the word "any" - differences in platform implementation might make it hard to define a cross-platform interface that works across all the platforms for any one of NFC, Bluetooth, etc, let alone all of them.

这篇关于为任务/意图提供单跨平台支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆