void UpdateItems(System.Windows.Controls.Grid grid)
        {
            grid.Children.Clear();

            grid.RowDefinitions.Clear();

            var x = 0;
            var y = 0;

            foreach (Page target in Element.Targets)
            {
                if (x > 1)
                {
                    x = 0;
                    y++;
                }

                if (x == 0)
                {
                    grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition());
                }

                var hubTile = new HubTile {
                    Title = target.Title, Source = new BitmapImage(new Uri(target.Icon, UriKind.Relative)), Margin = new System.Windows.Thickness(0, 0, Spacing, Spacing)
                };

                if (target.BackgroundColor != Color.Default)
                {
                    hubTile.Background = target.BackgroundColor.ToBrush();
                }

                Page tmp = target;
                hubTile.Tap += (sender, args) => Element.SendTargetSelected(tmp);

                hubTile.SetValue(System.Windows.Controls.Grid.RowProperty, y);
                hubTile.SetValue(System.Windows.Controls.Grid.ColumnProperty, x);
                hubTile.Size = GetSize();

                var weakRef = new WeakReference(hubTile);
                SizeChanged += (sender, args) =>
                {
                    var hTile = (HubTile)weakRef.Target;
                    if (hTile != null)
                    {
                        hTile.Size = GetSize();
                    }
                    ((IVisualElementController)Element).NativeSizeChanged();
                };

                x++;
                grid.Children.Add(hubTile);
            }
        }
Ejemplo n.º 2
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (String.IsNullOrEmpty(Settings.CachedAuthenticationToken))
            {
                NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
                return;
            }

            if (e.NavigationMode == NavigationMode.Back)
            {
                return;
            }

            User.Text = "Hello " + Settings.Username;

            client =
                new SpeechServiceClient(
                    GetBinding(),
                    GetEndpointAddress("Speech"));

            client.RecognizeCompleted +=
                (o1, e1) =>
            {
                HideProgress();

                if (e1.Error != null)
                {
                    if (e1.Error.GetType() == typeof(FaultException <AuthenticationFault>))
                    {
                        GetAuthenticationToken(
                            new Action(
                                () =>
                        {
                            client.RecognizeAsync(
                                Settings.CachedAuthenticationToken,
                                encodedFrames.ToArray(),
                                false,
                                null,
                                null);
                        }
                                ));
                    }
                    else
                    {
                        MessageBox.Show(e1.Error.Message);
                    }

                    return;
                }

                if (e1.Result.AnswerSpeech != null && e1.Result.AnswerSpeech.Length > 0)
                {
                    // Play the audio in a new thread so the UI can update.
                    new Thread(
                        () =>
                    {
                        byte[] speechBuffer = Speex.DecodeAllFrames(e1.Result.AnswerSpeech);
                        //SoundEffect sound = new SoundEffect(speechBuffer, 24000, AudioChannels.Mono);//microphone.SampleRate, AudioChannels.Mono);
                        //soundInstance = sound.CreateInstance();
                        //soundInstance.Play();
                    }).Start();
                }

                // TODO:
                MessageBox.Show(String.Format("I am {0}% confident you said \"{1}\". To which I reply \"{2}\".", e1.Result.Confidence * 100, e1.Result.Command, e1.Result.AnswerText));
            };

            int counter = 0;

            foreach (var endpoint in Settings.Endpoints)
            {
                if (endpoint.Key == "Speech" || endpoint.Key == "Recipes")
                {
                    continue;
                }

                HubTile tile =
                    new HubTile()
                {
                    Margin = new Thickness(12, 12, 0, 0),
                    Source = new BitmapImage(new Uri("/Images/" + endpoint.Key + ".png", UriKind.Relative)),
                    Name   = endpoint.Key,
                    //Title = endpoint.Key,
                    IsFrozen = true,
                    //Background = (SolidColorBrush)Resources["PhoneAccentBrush"],
                    //Message = "Message",
                    //DisplayNotification = true,
                    //Notification = "Notification",
                };
                tile.SetValue(Grid.ColumnProperty, counter % 2);
                tile.SetValue(Grid.RowProperty, counter / 2);

                tile.Tap +=
                    (o1, e1) =>
                {
                    NavigationService.Navigate(new Uri("/" + ((HubTile)o1).Name + "Page.xaml", UriKind.Relative));
                };

                tile.MouseLeftButtonDown +=
                    (o1, e1) =>
                {
                    System.Windows.Point tmpPoint = e1.GetPosition(null);
                    contextMenuSelectedHubTile = null;
                    List <UIElement> oControls = (List <UIElement>)VisualTreeHelper.FindElementsInHostCoordinates(tmpPoint, this);
                    foreach (UIElement ctrl in oControls)
                    {
                        if (ctrl is HubTile)
                        {
                            contextMenuSelectedHubTile = (HubTile)ctrl;
                            break;
                        }
                    }
                };

                TileGrid.Children.Add(tile);

                counter++;
            }

            HideProgress();
        }
Ejemplo n.º 3
0
        public CamerasPage()
        {
            InitializeComponent();

            client =
                new CameraServiceClient(
                    GetBinding(),
                    GetEndpointAddress("Cameras"));

            client.ListCamerasCompleted +=
                (o1, e1) =>
            {
                HideProgress();

                if (e1.Error != null)
                {
                    if (e1.Error.GetType() == typeof(FaultException <AuthenticationFault>))
                    {
                        GetAuthenticationToken(
                            new Action(
                                () =>
                        {
                            client.ListCamerasAsync(Settings.CachedAuthenticationToken);
                        }
                                ));
                    }
                    else
                    {
                        MessageTextBlock.Text = "Error retrieving camera list.";
                        MessageBox.Show(e1.Error.Message);
                    }

                    return;
                }

                if (e1.Result == null || e1.Result.Length == 0)
                {
                    MessageTextBlock.Text = "You have not set up any cameras. Press the add button to add a camera to the system.";
                }
                else
                {
                    MessageTextBlock.Visibility = System.Windows.Visibility.Collapsed;
                }

                int counter = 0;
                foreach (var a in e1.Result)
                {
                    HubTile tile =
                        new HubTile()
                    {
                        Title = a.Location,
                        Tag   = String.Format(
                            CultureInfo.InvariantCulture,
                            "?location={0}&mjpegurl={1}&username={2}&password={3}",
                            a.Location,
                            HttpUtility.UrlEncode(a.MjpegUrl),
                            a.Username,
                            a.Password),
                        Source =
                            new BitmapImage(
                                new Uri(
                                    a.ImageUrl.Insert(
                                        7,
                                        String.Format(
                                            CultureInfo.InvariantCulture,
                                            "{0}:{1}@",
                                            a.Username,
                                            a.Password)),
                                    UriKind.Absolute)),
                        Margin = new Thickness(12, 12, 0, 0),
                    };

                    tile.SetValue(Grid.ColumnProperty, counter % 2);
                    tile.SetValue(Grid.RowProperty, counter / 2);

                    tile.Tap +=
                        (o2, e2) =>
                    {
                        NavigationService.Navigate(new Uri("/SingleCameraPage.xaml" + (((HubTile)o2).Tag.ToString()), UriKind.Relative));
                    };

                    CameraGrid.Children.Add(tile);

                    counter++;
                }
            };
        }