【发布时间】:2017-07-03 06:16:42
【问题描述】:
最近,我一直在努力从 UI 对象获取和设置数据。 我知道可以通过使用 Invoking 从 BackgroundWorker-Thread 执行此操作。可悲的是,我只找到了调用方法,它可以很好地设置标签和其他一些东西,但是当涉及到 DataGridViews、Combo-和 TextBoxes 时它失败了。这是我正在谈论的调用“代码”:
this.uiObject.Invoke((MethodInvoker)delegate
{
this.uiObject.Text = "Hello World";//Setting Label Text from BackgroundWorker
});
正如我所说,我尝试在以下代码中使用它,但它不起作用。
private void loadPlaylists()
{
this.playlistGrid.Rows.Clear();//Invoke on a DataGridView
string filePath = this.genrePath + this.sortBox.SelectedItem.ToString() + ".txt";
if (File.Exists(filePath) && File.ReadAllText(filePath) != "")
{
using (StreamReader sr = new StreamReader(filePath))
{
bool first = false;
string line = "";
while ((line = sr.ReadLine()) != null)
{
if (first && line != "")
{
string[] split = line.Split(new string[] { " // " }, StringSplitOptions.None);
FullPlaylist playList = spotify.GetPlaylist(split[1], split[0]);
this.playlistGrid.Rows.Add(playList.Name, playList.Owner.Id);//Invoke on a DataGridView
}
if (line != "")
first = true;
}
}
}
}
private void loadItems(bool newItem = false, bool deletedItem = false, string name = "")
{
this.sortBox.Items.Clear();//Invoke on a ComboBox
DirectoryInfo dir = new DirectoryInfo(this.genrePath);
foreach (var file in dir.GetFiles("*.txt"))
{
string[] split = file.Name.Split(new string[] { ".txt" }, StringSplitOptions.None);
this.sortBox.Items.Add(split[0]);//Invoke on a ComboBox
}
if (newItem)
{
this.sortBox.SelectedItem = name;//Invoke on a ComboBox
this.mode = 5;
}
if (deletedItem)
{
if (this.sortBox.Items.Count > 0)//Invoke on a ComboBox
{
this.sortBox.SelectedIndex = 0;//Invoke on a ComboBox
this.mode = 5;
}
else
this.playlistGrid.Rows.Clear();//Invoke on a DataGirdView
}
}
private void addPlaylists()
{
string[] split;
string filePath = "";
if (this.sortBox.SelectedIndex != -1)//Invoke on a ComboBox
{
filePath = this.genrePath + this.sortBox.SelectedItem.ToString() + ".txt";//Invoke on a ComboBox
}
else
{
MetroFramework.MetroMessageBox.Show(this, "Select a valid Category first!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (this.playlistLink.Text != "" && this.playlistLink.Text.Contains("/"))//Invoke on a TextBox
{
if (this.playlistLink.Text.Contains("|"))//Invoke on a TextBox
{
split = this.playlistLink.Text.Split('|');//Invoke on a TextBox
}
else
{
split = new string[1];
split[0] = this.playlistLink.Text;//Invoke on a TextBox
}
for (int j = 0; j < split.Length; j++)
{
string[] split2 = split[j].Split('/');
string id = split2[split2.Length - 1], owner = split2[split2.Length - 3];
FullPlaylist playlist = this.spotify.GetPlaylist(owner, id);
string write = id + " // " + owner + " // " + playlist.Name;
this.changeOrAddLine(filePath, write, write);
}
this.playlistLink.Text = "";//Invoke on a TextBox
this.loadPlaylists();//Call to a Methode, where Invoke is needed
}
else if (!this.playlistLink.Text.Contains("/"))//Invoke on a TextBox
{
//Error
this.playlistLink.Text = "";//Invoke on a TextBox
}
else
{
//Error
}
}
这是我从 BackgrounddWorker 调用的三个方法。代码很好,可以在 BackgroundWorker 之外工作。我标记了应该使用调用(如果甚至需要)的所有行,因为它正在使用 UI 项目做一些事情。我希望你们中的某个人能够告诉我如何做到这一点。我不希望您使用我的代码并添加修复程序,只是一个关于如何执行此操作的总体示例也应该可以工作。提前致谢!
【问题讨论】:
标签: c# user-interface backgroundworker