我一直在尝试制作用户从PhotoChooserTask中选择的图片的副本,并将其用作我应用程序中的hubtile的背景图像,但我无法找到实现此目的的正确方法。截至目前,我可以将hubtile图像设置为PhotoChooserTask_Completed响应,但我不知道如何对Bitmap执行复制操作,以便我可以在再次启动或重新激活应用程序时保存此图像。到目前为止,我所拥有的内容如下:
我创建了一个自定义TileItem类来管理我的HubTiles的数据
TileItem.cs
public class TileItem
{
//Old Implementation
//public string ImageUri { get; set; }
//New Implementation
public BitmapImage ImageUri { get; set; }
public string Title { get; set; }
public string Notification { get; set; }
public bool DisplayNotification
{
get { return !string.IsNullOrEmpty(this.Notification); }
}
public string Message { get; set; }
public string GroupTag { get; set; }
//for translation purposes (bound to HubTile Title on MainPage)
public string TileName { get; set; }
MainPage.xaml.cs中
PhotoChooserTask photoChooserTask;
TileItem tileItem;
public MainPage()
{
InitializeComponent();
CreateHubTiles();
photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
}
接下来,我创建了磁贴,并使用自定义的“设置”类来保存我的数据(即“ImageUri&#39;现在是位图”)
public void CreateHubTiles()
{
List<TileItem> tileItems = new List<TileItem>()
{
new TileItem() { ImageUri = Settings.shareImage.Value, Title = "status", /*Notification = "last shared link uri",*/ Message = Settings.statusMessage.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Status_Title },
new TileItem() { ImageUri = Settings.linkImage.Value, Title = "link", /*Notification = "last shared status message",*/ Message = Settings.linkUri.Value, GroupTag = "TileGroup", TileName = AppResource.Main_MainPage_HubTile_Link_Title },
};
this.tileList.ItemsSource = tileItems;
}
然后我会处理PhotoChooserTask处理程序
public void changePictureMenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
var menuItem = (MenuItem)sender;
tileItem = menuItem.DataContext as TileItem; //used in PhotoChooserTask_Completed
try
{
photoChooserTask.Show();
}
catch (System.InvalidOperationException ex)
{
//MessageBox.Show("An error occurred");
MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_Error_Message);
}
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//BitmapImage bitmap = new BitmapImage();
//bitmap.SetSource(e.ChosenPhoto);
//get the correct tileItem that was clicked and set image source to respective hubtile
string tileTitle = tileItem.Title.ToString();
switch (tileTitle)
{
case "status":
tileItem.ImageUri.SetSource(e.ChosenPhoto); //changes the hubtile image to selected image
//Settings.shareImage.Value.Equals(e.ChosenPhoto);
//Set the selected image to the shareImage bitmap in the Settings class to persist image
Settings.shareImage.Value.Equals(tileItem.ImageUri); //not working!
break;
case "link":
//Same as above
tileItem.ImageUri.SetSource(e.ChosenPhoto);
//Settings.linkImage.Value.Equals(e.ChosenPhoto);
Settings.linkImage.Value.Equals(tileItem.ImageUri);
break;
}
}
else if (e.TaskResult == TaskResult.Cancel)
//MessageBox.Show("No picture was chosen - operation was cancelled", "Picture not chosen", MessageBoxButton.OK);
MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultCancel_Message, AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultCancel_Caption, MessageBoxButton.OK);
else
//MessageBox.Show("Error while choosing photo:\n" + e.Error.Message, "Fail", MessageBoxButton.OK);
//MessageBox.Show("Error while choosing picture", "Fail", MessageBoxButton.OK);
MessageBox.Show(AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultError_Message, AppResource.Main_MainPage_ContextMenu_ChangePicture_TaskResultError_Caption, MessageBoxButton.OK);
}
最初设置Settings.shareImage.Value
和Settings.linkImage.value
,但在PhotoChooserTask_Completed事件中未相应更改或更新。
Settings.cs
public static readonly Setting<BitmapImage> shareImage = new Setting<BitmapImage>("shareImage", new BitmapImage(new Uri("/Images/shareStatusImage.jpg", UriKind.Relative)));
public static readonly Setting<BitmapImage> linkImage = new Setting<BitmapImage>("linkImage", new BitmapImage(new Uri("/Images/shareLinkImage.jpg", UriKind.Relative)));
因此,图像仅在应用程序未关闭时更新(在PhotoChooserTask_Completed事件中),然后在重新启动或激活时,它们将被设置回原始图像。我如何相应地处理这些图像,以便PhotoChooserTask中的所选图像可以永久用作每个TileItem的新ImageUri
(现在实际上是根据我的第一个类的Bitmap)?
答案 0 :(得分:2)
如果我理解你,你需要找到一种方法将拍摄的图像保存到隔离存储并加载下次启动的应用程序。看看http://www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-Images,它包含保存和加载位图图像的完整代码