我想使用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);
}
}
我的结果是,作为开始屏幕上可用的最小图块,我看到应用程序图块,而中型和大图块尺寸仅显示空白图块(只是重点颜色和应用程序名称)。