C# 我的磁贴创建和加载有什么问题?
我已经制作了一个应用程序,它使用活动的瓷砖。我让它与背景任务一起工作,它显示正确的信息,但图像是完全黑色的 这是我的代码,我将图像保存到C# 我的磁贴创建和加载有什么问题?,c#,windows-phone-7,C#,Windows Phone 7,我已经制作了一个应用程序,它使用活动的瓷砖。我让它与背景任务一起工作,它显示正确的信息,但图像是完全黑色的 这是我的代码,我将图像保存到/Shared/ShellContent/: public MainPage() { InitializeComponent(); CurrentPlaceList = new ObservableCollection<CurrentPlaceListItemModel>
/Shared/ShellContent/
:
public MainPage()
{
InitializeComponent();
CurrentPlaceList = new ObservableCollection<CurrentPlaceListItemModel>();
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
var bmp = new WriteableBitmap(173, 173);
var weatherImage = new BitmapImage(new Uri("/Images/WeatherIcons/01d.png", UriKind.Relative));
var img = new Image { Source = weatherImage };
weatherImage.CreateOptions = BitmapCreateOptions.None;
var tt = new TranslateTransform();
tt.X = 0;
tt.Y = 0;
bmp.Render(img, tt);
bmp.Invalidate();
var filename = "/Shared/ShellContent/01d.jpg";
using (var st = new IsolatedStorageFileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, store))
{
bmp.SaveJpeg(st, 173, 173, 0, 100);
}
}
StartAgent();
}
这是从几本教程中摘取的,有人能指出哪里出了问题吗?对我来说,图像似乎没有足够的时间加载。我认为更好的方法是这样做:
StreamResourceInfo streamImage = Application.GetResourceStream(uri.Uri);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(streamImage.Stream);
Image image = new Image() { Width = uri.Width, Height = uri.Height, Source = bitmapImage };
此外,您还可以查看一个MSP工具包,它可以为您生成互动程序(NuGet上的msptoolkit) 在将图像渲染到
bmp
之前,可能需要等待ImageLoaded
事件,或者只将图像复制到/Shared/ShellContent/
目录,而不创建其他图像
对象。此外,您还可以查看独立存储
,查看此处保存的内容
StreamResourceInfo streamImage = Application.GetResourceStream(uri.Uri);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(streamImage.Stream);
Image image = new Image() { Width = uri.Width, Height = uri.Height, Source = bitmapImage };