【发布时间】:2014-10-09 21:50:11
【问题描述】:
我正在使用对象数据源编写一个 facebook win 应用程序。 表格显示所选相册中的照片, 对于列表框中选择的每张照片,您会在组框中看到照片的详细信息。 调试时抛出“设置 DataSource 属性时无法修改项目集合。”
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Facebook;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;
namespace FacebookWinApp
{
public class FormAlbumPictures : Form
{
private Label labelChoosePicture;
private ListBox listBoxPictures;
private PictureBox pictureBoxPictureFromAlbum;
private Button buttonOK;
private Button buttonCancel;
private BindingSource photoBindingSource;
private IContainer components;
private GroupBox groupBoxPhotoDetails;
private Label labelCurrentPhotoPlaceName;
private DataGridView dataGridViewCurrentPhotoTags;
private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
private DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
private BindingSource tagsBindingSource;
private Label labelCurrentPhotoCreatedTime;
private Label labelCurrentPhotoHeight;
private Label labelCurrentPhotoName;
private Label labelCurrentPhotoUpdateTime;
private Label labelCurrentPhotoWidth;
private Photo m_ChoosenPhoto = null;
public Photo ChoosenPhoto
{
get { return m_ChoosenPhoto; }
}
public User UserLoggedIn { get; set; }
public Album AlbumName { get; set; }
public FormAlbumPictures()
{
this.InitializeComponent();
}
public void FetchPhotosFromAlbum()
{
var allPhotosFromAlbum = AlbumName.Photos;
if (!listBoxPictures.InvokeRequired)
{
photoBindingSource.DataSource = allPhotosFromAlbum;
}
else
{
listBoxPictures.Invoke(new Action(() => photoBindingSource.DataSource = allPhotosFromAlbum));
}
listBoxPictures.Invoke(new Action(() =>
{
listBoxPictures.DisplayMember = "UpdateTime";
foreach (Photo photo in AlbumName.Photos)
{
listBoxPictures.Items.Add(photo); /// Here the exception thrown
}
}));
}
private void listBoxPictures_SelectedIndexChanged(object sender, EventArgs e)
{
displaySelectedPhoto();
}
private void displaySelectedPhoto()
{
Photo selectedPhoto = null;
if (listBoxPictures.SelectedItems.Count == 1)
{
selectedPhoto = listBoxPictures.SelectedItem as Photo;
if (selectedPhoto.PictureNormalURL != null)
{
pictureBoxPictureFromAlbum.LoadAsync(selectedPhoto.PictureNormalURL);
}
}
}
private void buttonOK_Click(object sender, EventArgs e)
{
if (listBoxPictures.SelectedItem != null)
{
m_ChoosenPhoto = listBoxPictures.SelectedItem as Photo;
}
this.DialogResult = DialogResult.OK;
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
}
我应该怎么做才能解决这个问题?
【问题讨论】:
标签: c# facebook multithreading winforms data-binding