SecondaryTile 构造函数的 ArgumentException
ArgumentException for SecondaryTile constructor
当我试图在 PinToStartAsync
中创建一个 SecondaryTile
时,我得到了一个 ArgumentException
。哪些参数使用不正确?
public const string LogoPath = "ms-appx:///Assets/monotone_no_bg.png";
public static async Task<bool> PinToStartAsync(Playlist playlist, bool isPlaylist)
{
string tilename = playlist.Name;
string tileid = isPlaylist ? tilename : $"{tilename}+++{playlist.Artist}";
string path = LogoPath;
if (playlist.DisplayItem.Source != null && await SecondaryTileFolder.TryGetItemAsync(tilename) == null)
{
StorageFile thumbnail = await (await GetStorageItemThumbnailAsync(playlist.DisplayItem.Source.Path)).SaveAsync(SecondaryTileFolder, tilename);
path = thumbnail.Path;
}
var tile = new SecondaryTile(tileid, tilename, isPlaylist.ToString(), new Uri(path), TileSize.Default);
// Other code
}
// This function saves a StorageItemThumbnail and returns a StorageFile
public static async Task<StorageFile> SaveAsync(this StorageItemThumbnail thumbnail, StorageFolder folder, string name)
{
using (var stream = thumbnail.CloneStream())
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
var filename = $"{name}.png";
var item = await folder.TryGetItemAsync(filename);
var file = await folder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (item == null)
{
using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, filestream);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
}
}
return file;
}
}
更多源代码在底部here。
问题出在您的缩略图上 link。
Images can be used from the app's package, the app's local storage, or from the web.
—— tile document
文件路径是通过thumbnail.Path
获取的,类似C:\Windows\...
,不允许。
在 SaveAsync 方法中,您在 LocalFolder
中创建了 {name}.png
。如果你想访问这个文件,你需要使用 link: ms-appdata:///local/{name}.png
.
此致。
当我试图在 PinToStartAsync
中创建一个 SecondaryTile
时,我得到了一个 ArgumentException
。哪些参数使用不正确?
public const string LogoPath = "ms-appx:///Assets/monotone_no_bg.png";
public static async Task<bool> PinToStartAsync(Playlist playlist, bool isPlaylist)
{
string tilename = playlist.Name;
string tileid = isPlaylist ? tilename : $"{tilename}+++{playlist.Artist}";
string path = LogoPath;
if (playlist.DisplayItem.Source != null && await SecondaryTileFolder.TryGetItemAsync(tilename) == null)
{
StorageFile thumbnail = await (await GetStorageItemThumbnailAsync(playlist.DisplayItem.Source.Path)).SaveAsync(SecondaryTileFolder, tilename);
path = thumbnail.Path;
}
var tile = new SecondaryTile(tileid, tilename, isPlaylist.ToString(), new Uri(path), TileSize.Default);
// Other code
}
// This function saves a StorageItemThumbnail and returns a StorageFile
public static async Task<StorageFile> SaveAsync(this StorageItemThumbnail thumbnail, StorageFolder folder, string name)
{
using (var stream = thumbnail.CloneStream())
{
var decoder = await BitmapDecoder.CreateAsync(stream);
var softwareBitmap = await decoder.GetSoftwareBitmapAsync();
var filename = $"{name}.png";
var item = await folder.TryGetItemAsync(filename);
var file = await folder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
if (item == null)
{
using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, filestream);
encoder.SetSoftwareBitmap(softwareBitmap);
await encoder.FlushAsync();
}
}
return file;
}
}
更多源代码在底部here。
问题出在您的缩略图上 link。
Images can be used from the app's package, the app's local storage, or from the web.
—— tile document
文件路径是通过thumbnail.Path
获取的,类似C:\Windows\...
,不允许。
在 SaveAsync 方法中,您在 LocalFolder
中创建了 {name}.png
。如果你想访问这个文件,你需要使用 link: ms-appdata:///local/{name}.png
.
此致。