基本上我正在尝试制作一个Windows Phone应用程序,其中可以在画布上添加逐个图像,但以下代码似乎无法正常工作
private void AddImage_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask chooseImage = new PhotoChooserTask();
chooseImage.Completed += chooseImage_Completed;
chooseImage.Show();
}
public void chooseImage_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
{
return;
}
Image img = new Image();
SelectedBitmap = new WriteableBitmap(60,60);
img.Width = 100;
img.Height = 100;
img.Name = "img";
SelectedBitmap.SetSource(e.ChosenPhoto);
img.Source = SelectedBitmap;
e.ChosenPhoto.Position = 0;
CollageCanvas.Children.Add(img);
Canvas.SetTop(img,50);
Canvas.SetLeft(img,50);
}
第一次单击该按钮时,图像已成功添加到画布,但是当我尝试将另一个图像添加到画布时,它会出现以下异常: -
"类型' System.ArgumentException'的例外情况发生在System.Windows.ni.dll中但未在用户代码中处理 附加信息:价值不在预期范围内。"
所以我想知道如何更改程序以便能够在画布上添加多个图像。
答案 0 :(得分:0)
我认为这与每个子元素的name属性必须是唯一的这一事实有关。我认为这对你有用。
private int index = 1;
public void chooseImage_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK || e.ChosenPhoto == null)
{
return;
}
Image img = new Image();
WriteableBitmap SelectedBitmap = new WriteableBitmap(60, 60);
img.Width = 100;
img.Height = 100;
img.Name = "img";
SelectedBitmap.SetSource(e.ChosenPhoto);
img.Source = SelectedBitmap;
img.Name = "Photo " + index++; // Set unique name here
e.ChosenPhoto.Position = 0;
CollageCanvas.Children.Add(img);
Canvas.SetTop(img, 50);
Canvas.SetLeft(img, 50);
}