private MobileServiceUser user; private async System.Threading.Tasks.Task Authenticate() { while (user == null) { string message; try { user = await App.MobileService .LoginAsync(MobileServiceAuthenticationProvider.Facebook); message = string.Format("You are now logged in - {0}", user.UserId); } catch (InvalidOperationException) { message = "You must log in. Login Required"; } MessageBox.Show(message); } }
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using myPhoneApp.Resources; using Microsoft.WindowsAzure.MobileServices; using Newtonsoft.Json; namespace myPhoneApp { public class MyMember { public int Id { get; set; } [JsonProperty(PropertyName = "name")] public string Name { get; set; } [JsonProperty(PropertyName = "email")] public string Email { get; set; } [JsonProperty(PropertyName = "status")] public string Status { get; set; } } public partial class MainPage : PhoneApplicationPage { private MobileServiceCollection<MyMember, MyMember> items; private IMobileServiceTable<MyMember> memberTable = App.MobileService.GetTable<MyMember>(); private MobileServiceUser user; // Constructor public MainPage() { InitializeComponent(); this.Loaded += MainPage_Loaded; } async void MainPage_Loaded(object sender, RoutedEventArgs e) { await Authenticate(); RefreshMemberItems(); } private async System.Threading.Tasks.Task Authenticate() { while (user == null) { string message; try { user = await App.MobileService .LoginAsync(MobileServiceAuthenticationProvider.Google); message = string.Format("You are now logged in - {0}", user.UserId); } catch (InvalidOperationException) { message = "You must log in. Login Required"; } MessageBox.Show(message); } } private async void RefreshMemberItems() { try { items = await memberTable.ToCollectionAsync(); } catch (MobileServiceInvalidOperationException e) { MessageBox.Show(e.Message, "Error loading items", MessageBoxButton.OK); } myListItems.ItemsSource = items; } } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท