Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6553159
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Android平台

2018-07-14 11:06:55

并行类和共享资产项目

虽然共享资产项目是平台项目的扩展,但这种关系是双向的:正如平台项目可以调用共享资产项目中的代码一样,SAP可以调用各个平台项目。
这意味着我们可以将特定于平台的API调用限制为各个平台项目中的类。 如果平台项目中这些类的名称和名称空间相同,那么SAP中的代码可以透明,独立于平台的方式访问这些类。
在PlatInfoSap2解决方案中,五个平台项目中的每一个都有一个名为PlatformInfo的类,它包含两个返回字符串对象的方法,名为GetModel和GetVersion。 这是iOS项目中此类的版本:

 

点击(此处)折叠或打开

  1. using System;
  2. using UIKit;
  3. namespace PlatInfoSap2
  4. {
  5.     public class PlatformInfo {
  6.         UIDevice device = new UIDevice();
  7.         public string GetModel()
  8.         { return device.Model.ToString();
  9.         }
  10.         public string GetVersion()
  11.         { return String.Format("{0} {1}", device.SystemName,
  12.             device.SystemVersion);
  13.         }
  14.     }
  15. }

注意命名空间名称。 虽然此iOS项目中的其他类使用PlatInfoSap2.iOS命名空间,但此类的命名空间仅为PlatInfoSap2。 这允许SAP直接访问此类,而无需任何平台细节。
这是Android项目中的并行类。 相同的命名空间,相同的类名和相同的方法名,但使用Android API调用的这些方法的不同实现:

 

点击(此处)折叠或打开

  1. using System;
  2. using Android.OS;
  3. namespace PlatInfoSap2
  4. {
  5.     public class PlatformInfo {
  6.         public string GetModel()
  7.         { return String.Format("{0} {1}", Build.Manufacturer,
  8.  Build.Model);
  9.         }
  10.         public string GetVersion()
  11.         { return Build.VERSION.Release.ToString();
  12.         }
  13.     }
  14. }

这是三个Windows和Windows Phone项目中三个相同副本中存在的类:

 

点击(此处)折叠或打开

  1. using System;
  2. using Windows.Security.ExchangeActiveSyncProvisioning;
  3. namespace PlatInfoSap2
  4. {
  5.     public class PlatformInfo {
  6.         EasClientDeviceInformation devInfo = new EasClientDeviceInformation();
  7.         public string GetModel()
  8.         { return String.Format("{0} {1}", devInfo.SystemManufacturer,
  9.  devInfo.SystemProductName);
  10.         }
  11.         public string GetVersion()
  12.         { return devInfo.OperatingSystem;
  13.         }
  14.     }
  15. }

PlatInfoSap2项目中的XAML文件与PlatInfoSap1项目中的XAML文件基本相同。 代码隐藏文件相当简单:

 

点击(此处)折叠或打开

  1. using System;
  2. using Xamarin.Forms;
  3. namespace PlatInfoSap2
  4. {
  5.     public partial class PlatInfoSap2Page : ContentPage {
  6.         public PlatInfoSap2Page ()
  7.         {
  8.             InitializeComponent ();
  9.             PlatformInfo platformInfo = new PlatformInfo();
  10.             modelLabel.Text = platformInfo.GetModel();
  11.             versionLabel.Text = platformInfo.GetVersion();
  12.         }
  13.     }
  14. }

该类引用的PlatformInfo的特定版本是已编译项目中的特定版本。 这几乎就像我们为Xamarin.Forms定义了一个小扩展,它存在于各个平台项目中。

阅读(5711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~