private void Unpin(AppTileSettings.TileSettings tileSettings, Uri navigationUri)
        {
            if (tileSettings == null && navigationUri != null)
            {
                tileSettings = AppTileSettings.Instance.GetTileSettings(navigationUri);
            }

            if (tileSettings != null)
            {
                navigationUri = tileSettings.NavigationUri;
                // slow impl. bored.
                if (AppTileSettings.Instance.Tiles.ContainsKey(navigationUri))
                {
                    AppTileSettings.Instance.Tiles.Remove(navigationUri);
                }
                AppTileSettings.Instance.Save();
            }

            if (navigationUri != null)
            {
                var tile = ShellTile.ActiveTiles.Where(
                        st => st.NavigationUri == navigationUri)
                        .SingleOrDefault();
                if (tile != null)
                {
                    tile.Delete();
                }
            }
        }
        public static TileInformation CreateFromTileSetting(AppTileSettings.TileSettings tileSettings)
        {
            var ti = new TileInformation();
            ti.OverwriteWithTileSettings(tileSettings);

            return ti;
        }
        public void OverwriteWithTileSettings(AppTileSettings.TileSettings tileSettings)
        {
            if (tileSettings != null)
            {
                TileSettingsInstance = tileSettings;

                Title = tileSettings.Title;
                BackgroundImage = tileSettings.FrontPhoto;

                // other methods are not yet supported.
            }
        }
        private void UpdatePrimaryAppIcon(AppTileSettings.TileSettings ts)
        {
            var type = ts.StyleType;

            if (string.IsNullOrEmpty(type))
            {
                // make sure tile pushes are on.
                ts.FrontPhoto = new Uri("/4thBackground_173.png", UriKind.Relative);

                SelectedMainIcon = null;
            }
            else
            {
                ts.ShellFrontPhotoPath = null;

                if (type == "static")
                {
                    ts.FrontPhoto = new Uri("/4thBackground_173.png", UriKind.Relative);

                    // disable live tile pushes.

                }
                else if (type == "classic")
                    {
                        ts.FrontPhoto = new Uri("/ClassicTile.png", UriKind.Relative);
                        // disable live tiles.

                    }
                else
                {
                    // enable live tile pushes.
                }

                SelectedMainIcon = type;
            }

            AppTileManager.Instance.UpdateOrPin(ts);
        }
        public void UpdateOrPin(AppTileSettings.TileSettings tileSettings)
        {
            Uri navigationUri = tileSettings.NavigationUri;

            AppTileSettings.TileSettings knownTileSettings = null;
            AppTileSettings.Instance.Tiles.TryGetValue(navigationUri, out knownTileSettings);

            var tile = ShellTile.ActiveTiles.Where(
                st => st.NavigationUri == navigationUri)
                .SingleOrDefault();
            if (tile != null && knownTileSettings == null)
            {
                // This tile needs to be deleted and then re-pinned, it's old.
                tile.Delete();
                tile = null;
            }

            // If there isn't an available shell path... Note a potential bug 
            // here, if you want to change an existing pin and the shell path
            // is set, this code path won't run and the pin won't actually
            // update. So clear it if you want to really change the front
            // photo path. For now I am not putting that into the data model
            // for the settings type. Ick.
            if (tileSettings.FrontPhoto != null && tileSettings.ShellFrontPhotoPath == null && tileSettings.FrontPhoto.IsAbsoluteUri)
            {
                if (tileSettings.FrontPhoto.Scheme == "http" || tileSettings.FrontPhoto.Scheme == "https")
                {
                    string expectedExtension = null;
                    var asString = tileSettings.FrontPhoto.ToString();
                    if (asString.Contains(".jpg"))
                    {
                        expectedExtension = ".jpg";
                    }
                    else if (asString.Contains(".png"))
                    {
                        expectedExtension = ".png";
                    }

                    StatusToken updateToken = null;

                    // Need to do some work to store it in the local isolated
                    // storage instead.
                    WebClient wc = new WebClient();
                    wc.OpenReadCompleted += (s, e) =>
                        {
                            if (updateToken != null)
                            {
                                StatusToken.TryComplete(ref updateToken);
                            }
                            var tt = tileSettings;
                            if (e != null)
                            {
                                if (e.Error != null)
                                {
                                    PriorityQueue.AddUiWorkItem(() =>
                                    {
                                        MessageBox.Show("The network connection was not available to download the image needed to create the tile. Please try again later.");
                                    });
                                    
                                    return;
                                }
                                else if (e.Result != null)
                                {
                                    byte[] bytes = null;
                                    using (var stream = e.Result)
                                    {
                                        bytes = new byte[stream.Length];
                                        stream.Read(bytes, 0, (int)stream.Length);
                                    }

                                    // GUID for this page.
                                    var path = Guid.NewGuid().ToString() + expectedExtension;
                                    var store = IsolatedStorageFile.GetUserStoreForApplication();
                                    string file = "Shared\\ShellContent\\" + path;

                                    Stream st = null;
                                    bool ok = false;

                                    try
                                    {
                                        store.EnsurePath(file);
                                        st = store.OpenFile(file, FileMode.Create, FileAccess.Write, FileShare.Read);
                                        st.Write(bytes, 0, bytes.Length);
                                        st.Flush();
                                        ok = true;
                                    }
                                    catch (IsolatedStorageException)
                                    {
                                        Debug.WriteLine("Exception writing file: Name={0}, Length={1}", file, bytes.Length);
                                    }
                                    catch (ObjectDisposedException)
                                    {
                                    }
                                    finally
                                    {
                                        if (s != null)
                                        {
                                            st.Close();
                                        }
                                    }

                                    if (ok)
                                    {
                                        Uri isoPath = new Uri("isostore:/Shared/ShellContent/" + path, UriKind.RelativeOrAbsolute);
                                        tt.ShellFrontPhotoPath = isoPath;
                                        UpdateOrPinForRealz(tile, navigationUri, tt);
                                    }
                                }
                            }
                        };
                    updateToken = CentralStatusManager.Instance.BeginShowEllipsisMessage("Preparing tile graphics");
                    wc.OpenReadAsync(tileSettings.FrontPhoto);
                    return;
                }
            }

            UpdateOrPinForRealz(tile, navigationUri, tileSettings);
        }
 public void Unpin(AppTileSettings.TileSettings tileSettings)
 {
     Unpin(tileSettings, null);
 }
        private void UpdateOrPinForRealz(ShellTile tile, Uri navigationUri, AppTileSettings.TileSettings tileSettings)
        {
            AppTileSettings.Instance.Tiles[navigationUri] = tileSettings;
            AppTileSettings.Instance.Save();

            var shellTileData = new StandardTileData
            {
                Title = tileSettings.Title,
                BackgroundImage = tileSettings.ShellFrontPhotoPath != null ? tileSettings.ShellFrontPhotoPath : tileSettings.FrontPhoto,
                BackBackgroundImage = tileSettings.BackPhoto,
            };

            if (tile == null)
            {
                // Pin.
                try
                {
                    ShellTile.Create(navigationUri, shellTileData);
                }
                catch (InvalidOperationException)
                {
                    AppTileSettings.Instance.Tiles.Remove(navigationUri);
                    AppTileSettings.Instance.Save();
                    MessageBox.Show("Unfortunately the tile could not be created at this time.");
                }
            }
            else
            {
                // Update.
                tile.Update(shellTileData);
            }
        }