我一直在尝试制作用户从 PhotoChooserTask 中选择的图片的副本,并将其用作我的应用程序中的 hubtile 的背景图像,但我无法找到更合适的方法来完成此操作。到目前为止,我可以将 hubtile 图像设置为 PhotoChooserTask_Completed 响应,但我不知道如何对位图执行复制操作,以便在再次启动或重新激活应用程序时保存此图像。到目前为止,我所拥有的如下:
我创建了一个自定义 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”)
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 事件中相应更改或更新。
设置.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 中选择的图像可以永久用作ImageUri
每个 TileItem 的新图像(根据我的第一个类,现在实际上是位图)?