// Constructor
        public PhotoSetAddPhotoView(PhotoSet setSource)
        {
            InitializeComponent();

            SelectedPhotos = new List<string>();
            PhotoSetSource = setSource;

            // Initialize data providers
            PhotoCollection = new ObservableCollection<SelectablePhoto>();
            PhotoListView.ItemsSource = PhotoCollection;

            // Renderers
            PhotoPickerRenderer.CanSelect = true;

            // Events
            Cinderella.CinderellaCore.PhotoStreamUpdated += OnPhotoStreamUpdated;

            Cinderella.CinderellaCore.AddPhotoToSetCompleted += OnAddPhotoCompleted;
            Anaconda.AnacondaCore.AddPhotoToSetException += OnAddPhotoException;

            Cinderella.CinderellaCore.RemovePhotoFromSetCompleted += OnRemovePhotoCompleted;
            Anaconda.AnacondaCore.RemovePhotoFromSetException += OnRemovePhotoException;

            PhotoPickerRenderer.SelectionChanged += OnPhotoPickerToggled;

            // Get group info
            StatusLabel.Text = AppResources.PhotoCollectionLoadingText;
            StatusProgressBar.Visibility = Visibility.Visible;
            Anaconda.AnacondaCore.GetPhotoStreamAsync(Cinderella.CinderellaCore.CurrentUser.ResourceId, new Dictionary<string, string> { { "page", "1" }, { "per_page", "40" } });
        }
        public void OnPopupRemoved()
        {
            Cinderella.CinderellaCore.PhotoSetPhotosUpdated -= OnPhotoStreamUpdated;
            Cinderella.CinderellaCore.PhotoSetPrimaryChanged -= OnPrimaryPhotoChanged;
            Anaconda.AnacondaCore.PhotoSetChangePrimaryException -= OnPrimaryPhotoException;

            PhotoListView.ItemsSource = null;

            PhotoCollection.Clear();
            PhotoCollection = null;

            PhotoSetSource = null;
        }
        public static PhotoSet PhotoSetWithJObject(JObject json)
        {
            string setId = json["id"].ToString();
            PhotoSet photoset;
            if (Cinderella.CinderellaCore.PhotoSetCache.ContainsKey(setId))
            {
                photoset = Cinderella.CinderellaCore.PhotoSetCache[setId];
            }
            else
            {
                photoset = new PhotoSet();
                photoset.ResourceId = setId;
                Cinderella.CinderellaCore.PhotoSetCache[setId] = photoset;
            }

            photoset.Primary = json["primary"].ToString();
            photoset.Secret = json["secret"].ToString();
            photoset.Server = json["server"].ToString();
            photoset.Farm = json["farm"].ToString();
            photoset.PhotoCount = int.Parse(json["photos"].ToString());
            photoset.VideoCount = int.Parse(json["videos"].ToString());
            photoset.Title = json["title"]["_content"].ToString();
            photoset.Description = json["description"]["_content"].ToString();
            photoset.IsVisible = json["visibility_can_see_set"].ToString().ParseBool();
            photoset.ViewCount = int.Parse(json["count_views"].ToString());
            photoset.CommentCount = int.Parse(json["count_comments"].ToString());
            photoset.CanComment = json["can_comment"].ToString().ParseBool();
            photoset.CreationDate = json["date_create"].ToString().ToDateTime();
            photoset.UpdatedDate = json["date_update"].ToString().ToDateTime();

            // Create the primary photo
            if (Cinderella.CinderellaCore.PhotoCache.ContainsKey(photoset.Primary))
            {
                photoset.PrimaryPhoto = Cinderella.CinderellaCore.PhotoCache[photoset.Primary];
            }
            else
            {
                Photo primaryPhoto = new Photo();
                primaryPhoto.ResourceId = photoset.Primary;
                primaryPhoto.Secret = photoset.Secret;
                primaryPhoto.Server = photoset.Server;
                primaryPhoto.Farm = photoset.Farm;

                photoset.PrimaryPhoto = primaryPhoto;
                Cinderella.CinderellaCore.PhotoCache[photoset.Primary] = primaryPhoto;
            }

            return photoset;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (executedOnce)
                return;

            executedOnce = true;

            // Events
            InitializeEventListeners();

            string setId = NavigationContext.QueryString["photoset_id"];
            PhotoSetSource = Cinderella.CinderellaCore.PhotoSetCache[setId];
            rendererFactory = new CommonPhotoGroupFactory();
            rendererFactory.Context = PhotoSetSource.ResourceId;
            rendererFactory.ContextType = "PhotoSet";

            PerformAppearanceAnimation();
        }
        // Constructor
        public PhotoSetPrimaryChooserView(PhotoSet setSource)
        {
            InitializeComponent();

            PhotoSetSource = setSource;

            // Initialize data providers
            PhotoCollection = new ObservableCollection<Photo>();
            foreach (var photo in PhotoSetSource.Photos)
            {
                PhotoCollection.Add(photo);
            }

            PhotoListView.ItemsSource = PhotoCollection;

            // Events
            Cinderella.CinderellaCore.PhotoSetPhotosUpdated += OnPhotoStreamUpdated;
            Cinderella.CinderellaCore.PhotoSetPrimaryChanged += OnPrimaryPhotoChanged;
            Anaconda.AnacondaCore.PhotoSetChangePrimaryException += OnPrimaryPhotoException;
        }
        public void OnPopupRemoved()
        {
            Cinderella.CinderellaCore.PhotoStreamUpdated -= OnPhotoStreamUpdated;

            Cinderella.CinderellaCore.AddPhotoToSetCompleted -= OnAddPhotoCompleted;
            Anaconda.AnacondaCore.AddPhotoToSetException -= OnAddPhotoException;

            Cinderella.CinderellaCore.RemovePhotoFromSetCompleted -= OnRemovePhotoCompleted;
            Anaconda.AnacondaCore.RemovePhotoFromSetException -= OnRemovePhotoException;

            PhotoPickerRenderer.SelectionChanged -= OnPhotoPickerToggled;

            PhotoListView.ItemsSource = null;

            PhotoCollection.Clear();
            PhotoCollection = null;

            SelectedPhotos.Clear();
            SelectedPhotos = null;

            PhotoSetSource = null;
        }