我想使用 Windows Phone 8 中提供的新的 Cycle Tile 功能,尽管我在动态创建它时遇到了麻烦。尝试为 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);
}
}
据我了解,Cycle Tile 的平铺图像应保存在 IsolatedStorage 的 ShellTile 目录中。为此,我正在使用以下我快速修改的类(我真的只使用SaveToLocalStorage
方法)
TilePictureRepository.cs
#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);
}
}
我的结果是,作为开始屏幕上可用的最小磁贴,我看到了应用磁贴,而中等和大磁贴尺寸仅显示空白磁贴(仅显示强调色和应用程序名称)。