windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

栏目: 前端 · 发布时间: 6年前

内容简介:windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

在使用azure之前,我一直只能做本地app,或者使用第三方提供的api,尽管大多数情况下够用,但是仍不能随心所欲操纵数据,这种感觉不是特别好。于是在azure发布后,我就尝试使用azure来做为个人数据中心,可选的方式有很多,但今天我给大家介绍的是azure mobile service。

1.创建Mobile Service

Azure中创建Mobile Service很简单,与创建其他项目类似,流程如下:

i.

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

ii.

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

iii.

ii windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥 windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

这里我使用的是Windows Azure国际版,经过简单的流程就能创建一个Mobile Service, 需要注意,当前服务还是空的,需要我们部署相应的Web API Service。

2.部署Web API服务

创建后的空Mobile Service仪表板如下:

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

我们在选择平台那一块栏目,选择Windows Phone,这时可以看到入门指南,大家可以任意挑选一种,这里我们挑选“连接至现有Windows Phone应用程序”。

接下来需要创建并发布一个ASP.NET Web API项目至我们的Mobile Service,这里大家有两种选择:

1.从Visual Studio中全新创建,选择Web API或者Azure Mobile Service模板均可

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

2.或者从Azure仪表板中下载一个标准Azure Mobile Service项目工程,解压至本地编辑

以上两种方法最终都会得到一样的结果,一个ASP.NET Web API项目,如下:

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

如果大家对ASP.NET Web API项目熟悉的话,可以直接在Controller文件夹下找到核心控制器代码,在其中进行一系列的修改,生成自己的方法,如下:

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

在控制器类中可以看到相关API,大家可以自行尝试效果,默认返回的是Json格式数据。

GET /tables/todoitem

结果:

[{"id":"1","complete":false,"text":"First item"},{"id":"2","complete":false,"text":"Second item"}]

下面我们简单运行生成项目后,将其发布至Azure Mobile Service,如下:

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

发布完成后,我们来看运行效果,如下:

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

3.在Windows Phone项目中使用Mobile Service

打开我们的windows phone项目,使用NuGet安装 WindowsAzure.MobileServices SDK。这里要特别注意:Windows Azure Mobile Service SDK支持WinRT& Silverlight框架,所以基于WinRT的WP8.1项目完全可以使用。

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥

新建数据类TodoItem,结构应与Web API项目中定义的一致,如下:

public class TodoItem {
    public string Id { get; set; }
    public string Text { get; set; }
    public bool Complete { get; set; }
}

在App.xaml.cs中创建 MobileServiceClient实例,初始化参数分别为applicationUri和applicationKey,参数你们替换为自己的即可如下

public static MobileServiceClient mobileService = new MobileServiceClient("https://azuretestmobileservice.azure-mobile.net/",“{applicationKey}”);

下面我们就可以对云端数据进行获取,上传,增删改等操作,并将结果呈现到客户端UI中,我们可以简单操作一下,如下:

MainPage.xaml

<StackPanel>
			<ListView x:Name="TestDataList">
				<ListView.ItemTemplate>
					<DataTemplate>
						<StackPanel Orientation="Horizontal">
							<CheckBox IsChecked="{Binding Complete}"/>
							<TextBlock TextWrapping="Wrap" FontSize="30" Text="{Binding Text}"/>
						</StackPanel>
					</DataTemplate>
				</ListView.ItemTemplate>

			</ListView>
			<Button x:Name="insertButton" Click="insertButton_Click">插入记录</Button>
			<TextBox x:Name="inputTextBox" ></TextBox>
		</StackPanel>

MainPage.xaml.cs

protected async override void OnNavigatedTo(NavigationEventArgs e)
		{
			await loadAzureData();
		}

		private async void insertButton_Click(object sender, RoutedEventArgs e)
		{
			if (inputTextBox.Text.Trim() != null && inputTextBox.Text.Trim() != "")
			{
				TodoItem newItem = new TodoItem { Complete = false, Text = inputTextBox.Text.Trim() };
				await App.mobileService.GetTable<TodoItem>().InsertAsync(newItem);
			}
			await loadAzureData();
		}

		private  async Task loadAzureData()
		{
			if (todoItems == null)
				todoItems = new List<TodoItem>();
			todoItems = await App.mobileService.GetTable<TodoItem>().ToListAsync();
			TestDataList.ItemsSource = todoItems;
		}

最后效果,至此我们就顺利部署了一个Azure Mobile Service实例,并集成到windows phone 8&8.1项目中

windows phone开发-windows azure mobile service使用入门 - msp的昌伟哥哥


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

设计方法卡牌

设计方法卡牌

罗莎 等 / 电子工业出版社 / 2017-7 / 79.00

本套设计素材提供了54种设计方法,以卡牌的形式展示给读者,每张卡牌包括该设计方法的基本描述、目的、时间成本、工具渠道、使用阶段、操作步骤及其归类属性等信息。在做设计时,可以根据自己的需求进行卡牌的选择和组合,让设计工作更有灵活性和趣味性。同时,依据设计产品属性的不同,卡牌提供了选择设计方法的推荐模板、方法组合模板、产品阶段划分模板,给初学者在做设计时提供一定的理论依据,帮助读者启发设计灵感,剖析设......一起来看看 《设计方法卡牌》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具