Exemplo n.º 1
0
        public void GetSecondaryTileData(long userOrGroupId, bool isGroup, string name, int maxImages, Action <bool, CycleTileData> completionCallback, string smallPhoto = null)
        {
            //Func<Tuple<int, string>, bool> func1;
            //Func<Tuple<int, string>, Uri> func2;
            this.GetRemoteUrisFor(Math.Abs(userOrGroupId), isGroup, (Action <bool, List <string> >)((res, resData) =>
            {
                if (res)
                {
                    List <Tuple <int, string> > imagesToDownloadData = new List <Tuple <int, string> >();
                    resData  = resData.Take <string>(maxImages).ToList <string>();
                    int num1 = 0;
                    foreach (string str in resData)
                    {
                        imagesToDownloadData.Add(new Tuple <int, string>(num1, str));
                        ++num1;
                    }
                    if (resData.Count == 0)
                    {
                        Action <bool, CycleTileData> action = completionCallback;
                        int num2 = 1;
                        CycleTileData cycleTileData = new CycleTileData();
                        string str = SecondaryTileManager.FormatNameForTile(name);
                        ((ShellTileData)cycleTileData).Title = str;
                        action(num2 != 0, cycleTileData);
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(smallPhoto))
                        {
                            imagesToDownloadData.Add(new Tuple <int, string>(1000, smallPhoto));
                        }
                        List <WaitHandle> waitHandles = SecondaryTileManager.DownloadImages(imagesToDownloadData, "UserOrGroup", userOrGroupId.ToString() + isGroup.ToString());
                        new Thread((ThreadStart)(() =>
                        {
                            if (!WaitHandle.WaitAll(waitHandles.ToArray(), 15000))
                            {
                                completionCallback(false, null);
                            }
                            else
                            {
                                CycleTileData cycleTileData = new CycleTileData();
                                string str = SecondaryTileManager.FormatNameForTile(name);
                                ((ShellTileData)cycleTileData).Title = str;
                                List <Uri> list = imagesToDownloadData.Where <Tuple <int, string> >(/*func1 ?? (func1 = (*/ new Func <Tuple <int, string>, bool>(im => im.Item2 != smallPhoto)).Select <Tuple <int, string>, Uri>(/*func2 ?? (func2 = (*/ new Func <Tuple <int, string>, Uri>(im => new Uri("isostore:/" + SecondaryTileManager.GetLocalNameFor(im.Item1, "UserOrGroup", userOrGroupId.ToString() + isGroup.ToString()), UriKind.Absolute))).ToList <Uri>();

                                cycleTileData.CycleImages = ((IEnumerable <Uri>)list);
                                Uri uri = smallPhoto == null ?  null : new Uri("isostore:/" + SecondaryTileManager.GetLocalNameFor(1000, "UserOrGroup", userOrGroupId.ToString() + isGroup.ToString()), UriKind.Absolute);
                                cycleTileData.SmallBackgroundImage = uri;
                                completionCallback(true, cycleTileData);
                            }
                        })).Start();
                    }
                }
                else
                {
                    completionCallback(false, null);
                }
            }));
        }
Exemplo n.º 2
0
        public static List <WaitHandle> DownloadImages(List <Tuple <int, string> > indexedUris, string tileType, string objId)
        {
            List <WaitHandle> waitHandleList = new List <WaitHandle>();

            foreach (Tuple <int, string> indexedUri in indexedUris)
            {
                int             ind          = indexedUri.Item1;
                string          str          = indexedUri.Item2;
                EventWaitHandle threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset);
                waitHandleList.Add((WaitHandle)threadFinish);
                Stream cachedImageStream = ImageCache.Current.GetCachedImageStream(str);
                string localName         = SecondaryTileManager.GetLocalNameFor(ind, tileType, objId);
                if (cachedImageStream != null)
                {
                    try
                    {
                        using (IsolatedStorageFileStream storageFileStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(localName, FileMode.Create, FileAccess.ReadWrite))
                        {
                            using (cachedImageStream)
                            {
                                byte[] buffer = new byte[1024];
                                while (cachedImageStream.Read(buffer, 0, buffer.Length) > 0)
                                {
                                    storageFileStream.Write(buffer, 0, buffer.Length);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.Error("SecondaryTileManager.DownloadImages failed", ex);
                    }
                    finally
                    {
                        threadFinish.Set();
                    }
                }
                else
                {
                    try
                    {
                        HttpWebRequest request = WebRequest.CreateHttp(str);
                        request.BeginGetResponse((AsyncCallback)(ir =>
                        {
                            try
                            {
                                WebResponse response = request.EndGetResponse(ir);
                                using (IsolatedStorageFileStream storageFileStream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(localName, FileMode.Create, FileAccess.ReadWrite))
                                {
                                    using (Stream responseStream = response.GetResponseStream())
                                    {
                                        byte[] buffer = new byte[1024];
                                        while (responseStream.Read(buffer, 0, buffer.Length) > 0)
                                        {
                                            storageFileStream.Write(buffer, 0, buffer.Length);
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                            finally
                            {
                                threadFinish.Set();
                            }
                        }), null);
                    }
                    catch (Exception)
                    {
                        threadFinish.Set();
                    }
                }
            }
            return(waitHandleList);
        }