I have been attempting to make a copy of a picture that the user selects from the PhotoChooserTask and use this as a background image for a hubtile in my application, but I cannot figure out the properr way to accomplish this. As of now, I can set the hubtile image to the PhotoChooserTask_Completed response but I do not know how to perform the copy action to a Bitmap so that I may save this image for when the application is launched again or reactivated. So far what I have is as follows:
I create a custom TileItem class to manage the data for my 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);
}
Next I create the tiles and use a custom Settings class to persist my data (namely the 'ImageUri' which is now a Bitmap)
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;
}
And then I take care of the PhotoChooserTask handler
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);
}
Originally Settings.shareImage.Value
and Settings.linkImage.value
are set, but are not changed or updated accordingly in the PhotoChooserTask_Completed event.
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)));
Therefore, the images are only updated (in the PhotoChooserTask_Completed event) while the app is not closed, and then upon relaunching or activation they are set back to the orginial images. How can I coppy these images accordingly so that the selected images from the PhotoChooserTask may then be used permanently as the new ImageUri
(which is now actually a Bitmap according to my first class) for each TileItem?