2012-09-24 22 views
2

我有WP7和Android上的應用程序,而這個應用程序必須有supporthas爲「任何」連接類型支持(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請求或任務請求,而無需任何人知道需要什麼類型的請求?

回答

5

這與MvvmCross允許用戶撥打電話的方式類似。

當使用這種模式:

視圖模型代碼經由接口消耗的平臺獨立的服務 - 例如:在https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/BaseViewModel.cs

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); 
    } 

消耗

的應用設置代碼注入平臺特定的i mplementation的界面 - 例如:

 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,藍牙等任何平臺的所有平臺,更不用說所有平臺。