Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Windows Phone 8中动态创建CycleTile_C#_Windows Phone 8_Isolatedstorage_Live Tile - Fatal编程技术网

C# 如何在Windows Phone 8中动态创建CycleTile

C# 如何在Windows Phone 8中动态创建CycleTile,c#,windows-phone-8,isolatedstorage,live-tile,C#,Windows Phone 8,Isolatedstorage,Live Tile,我想使用Windows Phone 8中提供的新的循环平铺功能,尽管我在动态创建此功能时遇到困难。我的问题出现在尝试为Cycle Tile设置图像时,我似乎没有获得图像的正确路径。到目前为止,我掌握的情况如下 MainPage.xaml.cs private void CreateApplicationTile() { //App.PictureList.Pictures grabs all of my application images from IsolatedSt

我想使用Windows Phone 8中提供的新的循环平铺功能,尽管我在动态创建此功能时遇到困难。我的问题出现在尝试为Cycle Tile设置图像时,我似乎没有获得图像的正确路径。到目前为止,我掌握的情况如下

MainPage.xaml.cs

private void CreateApplicationTile()
    {
        //App.PictureList.Pictures grabs all of my application images from IsolatedStorage
        int count = App.PictureList.Pictures.Count();
        int count1 = count - 9;
        var cycleImages = new List<Uri>();

        if (count > 0)
        {
            //I only want to add the 9 most recent images if available
            for (int i = count; i > count1; i--)
            {
                int index = i - 1;
                if (index > -1)
                {
                    //Set file to type CapturedPicture, which contains the jpg, name, date, etc.
                    var file = App.PictureList.Pictures[index] as CapturedPicture;
                    //TilePictureRepository class saves the file (picture) to "Shared/ShellContent/"
                    //TilePictureRepository.IsolatedStoragePath = "Shared/ShellContent/"
                    TilePictureRepository.Instance.SaveToLocalStorage(file, TilePictureRepository.IsolatedStoragePath);
                }
            }
        }


        // Select the application tile 
        ShellTile myTile = ShellTile.ActiveTiles.First();

        if (myTile != null)
        {
            Uri[] u = new Uri[9];
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IEnumerable<string> files = isf.GetFileNames(TilePictureRepository.IsolatedStoragePath + "*").Reverse();
                int x = 0;
                foreach (string file in files)
                {
                    if (x < 9)
                    {
                        u[x] = new Uri("isostore:/Shared/ShellContent/" + file, UriKind.Absolute);
                        cycleImages.Add(u[x]);
                        x++;
                    }
                }
            }

            CycleTileData newTileData = new CycleTileData
            {
                Title = "Tile Test",
                SmallBackgroundImage = new Uri("/Assets/Tiles/Tile_Small_159x159.png", UriKind.Relative),
                CycleImages = cycleImages,
            };

            myTile.Update(newTileData);
        }
    }

我的结果是,作为“开始”屏幕上可用的最小互动程序,我看到的是应用程序互动程序,而中、大互动程序大小只显示一个空白互动程序(仅显示重音颜色和应用程序名称)。

这可能会破坏很多地方。您真的需要通过调试器运行这个程序,并返回更多信息。是否无法将图片移动到shell目录?它是否无法获取当前磁贴?是不是一开始就找不到照片?如果没有任何类型的实际错误,调试的内容太多了。当应用程序离开主页时,我正在调用
createapplicationfile
方法
App.PictureList.Pictures
已经包含从隔离存储加载的图像,并且使用
tilepicturererepository.Instance.SaveToLocalStorage复制最近的9个图像似乎可以工作
isf.GetFileNames
检索图像,循环完成后,我可以看到
Uri
s匹配
“isostore:/Shared/ShellContent/”+file
中的
cycleImages
。正在检索互动程序,因为设置其他互动程序属性可以正常工作。我在调试时也没有看到任何错误。
#region Constants

    public const string IsolatedStoragePath = "Shared/ShellContent/";

    #endregion

    #region Fields

    private readonly ObservableCollection<Picture> _pictures = new ObservableCollection<Picture>();

    #endregion

    #region Properties

    public ObservableCollection<Picture> Pictures
    {
        get { return _pictures; }
    }

    #endregion

    #region Singleton Pattern

    private TilePictureRepository()
    {
        LoadAllPicturesFromIsolatedStorage();
    }

    public static readonly TilePictureRepository Instance = new TilePictureRepository();

    #endregion

    /// <summary>        
    /// Saves to local storage
    /// This method gets two parameters: the captured picture instance and the name of the pictures folder in the isolated storage
    /// </summary>
    /// <param name="capturedPicture"></param>
    /// <param name="directory"></param>
    public void SaveToLocalStorage(CapturedPicture capturedPicture, string directory)
    {
        //call IsolatedStorageFile.GetUserStoreForApplication to get an isolated storage file
        var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
        //Call the IsolatedStorageFile.EnsureDirectory extension method located in the Common IsolatedStorageFileExtensions class to confirm that the pictures folder exists.
        isoFile.EnsureDirectory(directory);

        //Combine the pictures folder and captured picture file name and use this path to create a new file 
        string filePath = Path.Combine(directory, capturedPicture.FileName);
        using (var fileStream = isoFile.CreateFile(filePath))
        {
            using (var writer = new BinaryWriter(fileStream))
            {
                capturedPicture.Serialize(writer);
            }
        }
    }

    /// <summary>
    /// To load all saved pictures and add them to the pictures list page
    /// </summary>
    public CapturedPicture LoadFromLocalStorage(string fileName, string directory)
    {
        //To open the file, add a call to the IsolatedStorageFile.GetUserStoreForApplication
        var isoFile = IsolatedStorageFile.GetUserStoreForApplication();

        //Combine the directory and file name
        string filePath = Path.Combine(directory, fileName);
        //use the path to open the picture file from the isolated storage by using the IsolatedStorageFile.OpenFile method
        using (var fileStream = isoFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
        {
            //create a BinaryReader instance for deserializing the CapturedPicture instance
            using (var reader = new BinaryReader(fileStream))
            {
                var capturedPicture = new CapturedPicture();
                //create a new instance of the type CapturedPicture called CapturedPicture.Deserialize to deserialize the captured picture and return it
                capturedPicture.Deserialize(reader);
                return capturedPicture;
            }
        }
    }

    /// <summary>
    /// To load all the pictures at start time
    /// </summary>
    private void LoadAllPicturesFromIsolatedStorage()
    {
        //add call to the IsolatedStorageFile.GetUserStoreForApplication to open an isolated storage file
        var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
        //Call the IsolatedStorageFile.EnsureDirectory extension method located in the Common IsolatedStorageFileExtensions class to confirm that the pictures folder exists
        isoFile.EnsureDirectory(IsolatedStoragePath);

        //Call the IsolatedStorageFile.GetFileNames using the pictures directory and *.jpg as a filter to get all saved pictures
        var pictureFiles = isoFile.GetFileNames(Path.Combine(IsolatedStoragePath, "*.jpg"));            

        //Iterate through all the picture files in the list and load each using the LoadFromLocalStorage you created earlier
        foreach (var pictureFile in pictureFiles)
        {
            var picture = LoadFromLocalStorage(pictureFile, IsolatedStoragePath);
            _pictures.Add(picture);
        }
    }