using System; using System.Data.Services.Client; using System.Text; using System.Linq; using System.Windows; using GravityWorks.DerbyApp.WP7.Service; using Microsoft.Phone.Controls; using Microsoft.Phone.Notification; using Microsoft.Phone.Shell; using Microsoft.Devices.Sensors; using System.Windows.Controls; namespace GravityWorks.DerbyApp.WP7 { public partial class MainPage : PhoneApplicationPage { readonly DerbyNamesEntities context = new DerbyNamesEntities(new Uri("http://localhost:1132/DerbyNames.svc/")); const string channelName = "DerbyNamesChannel"; Accelerometer acc; public MainPage() { InitializeComponent(); LoadApplicationBar(); //LoadTileInfo(); EnablePushNotifications(); //LoadAccelerometer(); Loaded += MainPage_Loaded; } private void LoadAccelerometer() { acc = new Accelerometer(); acc.ReadingChanged += OnAccelerometerReadingChanged; acc.Start(); } delegate void SetTextBlockTextDelegate(TextBlock txtblk, string text); void SetTextBlockText(TextBlock txtblk, string text) { txtblk.Text = text; } void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs args) { string str = String.Format("X = {0:F2}\nY = {1:F2}\nZ = {2:F2}\n\nMagnitude = {3:F2}\n\n{4}", args.X, args.Y, args.Z, Math.Sqrt(args.X * args.X + args.Y * args.Y + args.Z * args.Z), args.Timestamp); if (txtblk.CheckAccess()) { SetTextBlockText(txtblk, str); } else { txtblk.Dispatcher.BeginInvoke(new SetTextBlockTextDelegate(SetTextBlockText), txtblk, str); } } private static void LoadTileInfo() { StandardTileData data = new StandardTileData { Title = "Derby Names Tile", Count = 42, BackTitle = "Gravity Works", BackContent = "Derby Names App", //BackgroundImage = new Uri("/Background.png", UriKind.RelativeOrAbsolute), //BackBackgroundImage = new Uri("/Background.png", UriKind.RelativeOrAbsolute) }; ShellTile.ActiveTiles.First().Update(data); } private void EnablePushNotifications() { HttpNotificationChannel pushChannel = HttpNotificationChannel.Find(channelName); if (pushChannel == null) { pushChannel = new HttpNotificationChannel(channelName); pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated; pushChannel.ErrorOccurred += PushChannel_ErrorOccurred; pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived; pushChannel.HttpNotificationReceived += PushChannel_HttpNotificationReceived; pushChannel.Open(); pushChannel.BindToShellToast(); pushChannel.BindToShellTile(); } else { pushChannel.ChannelUriUpdated += PushChannel_ChannelUriUpdated; pushChannel.ErrorOccurred += PushChannel_ErrorOccurred; pushChannel.ShellToastNotificationReceived += PushChannel_ShellToastNotificationReceived; pushChannel.HttpNotificationReceived += PushChannel_HttpNotificationReceived; } System.Diagnostics.Debug.WriteLine(string.Format("{0}", pushChannel.ChannelUri)); } void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) { Dispatcher.BeginInvoke(() => { // Display the new URI. System.Diagnostics.Debug.WriteLine(string.Format("{0}", e.ChannelUri)); //MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri)); }); } void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e) { // Error handling logic for your particular application would be here. Dispatcher.BeginInvoke(() => MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))); } //Show the Toast Content when in the the app. void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e) { StringBuilder message = new StringBuilder(); string relativeUri = string.Empty; message.AppendFormat("Received Toast {0}:\n", DateTime.Now.ToShortTimeString()); // Parse out the information that was part of the message. foreach (string key in e.Collection.Keys) { message.AppendFormat("{0}: {1}\n", key, e.Collection[key]); if (string.Compare( key, "wp:Param", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.CompareOptions.IgnoreCase) == 0) { relativeUri = e.Collection[key]; } } Dispatcher.BeginInvoke(() => MessageBox.Show(message.ToString())); } void PushChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e) { string message; using (System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body)) { message = reader.ReadToEnd(); } Dispatcher.BeginInvoke(() => MessageBox.Show(String.Format("Received Notification {0}:\n{1}", DateTime.Now.ToShortTimeString(), message)) ); } private void LoadApplicationBar() { ApplicationBar = new ApplicationBar() { Mode = ApplicationBarMode.Minimized, Opacity = 1.0, IsVisible = true, IsMenuEnabled = true }; ApplicationBarIconButton button1 = new ApplicationBarIconButton() { IconUri = new Uri("/Toolkit.Content/ApplicationBar.Check.png", UriKind.Relative), Text = "button 1" }; ApplicationBar.Buttons.Add(button1); button1.Click += event_Click; ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem() { Text = "menu item 1" }; ApplicationBar.MenuItems.Add(menuItem1); menuItem1.Click += event_Click; } private void event_Click(object sender, EventArgs e) { MessageBox.Show("Alert"); App.accelerometer.Stop(); } private void MainPage_Loaded(object sender, RoutedEventArgs e) { LoadDerbyNames(); LoadLeagues(); } private void LoadDerbyNames() { var derbyNamesCollection = new DataServiceCollection(context); DerbyNamesList.ItemsSource = derbyNamesCollection; derbyNamesCollection.LoadCompleted += coll_LoadCompleted; var DerbyNamesQuery = "/DerbyNames"; derbyNamesCollection.LoadAsync(new Uri(DerbyNamesQuery, UriKind.Relative)); } private void LoadLeagues() { var derbyLeagueCollection = new DataServiceCollection(context); DerbyLeaguesList.ItemsSource = derbyLeagueCollection; derbyLeagueCollection.LoadCompleted += coll_LoadCompleted; var DerbyLeaguesQuery = "/Leagues"; derbyLeagueCollection.LoadAsync(new Uri(DerbyLeaguesQuery, UriKind.Relative)); } void coll_LoadCompleted(object sender, LoadCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message); } } } }