我做错了什么?我有正常运行的应用程序,但是当我按下Win 7中的按钮(在Google中搜索)时指定
hl.NavigateUri = new Uri("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=");
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
bool google = true;
if (((Button)sender).Content.ToString() == "Google")
google = true;
else
google = false;
Run run1 = new Run(textBox1.Text);
Hyperlink hl = new Hyperlink(run1);
TextBlock tb = new TextBlock();
//Label label1 = new Label();
//label1.Content = textBox1.Text;
//label1.Tag = ;
if (google)
hl.NavigateUri = new Uri("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=");
else
hl.NavigateUri = new Uri("http://yandex.ua/yandsearch?text=");
hl.Tag = i;
hl.Click += new RoutedEventHandler(hl_Click);
tb.Inlines.Add(hl);
tb.Inlines.Add(" ");
wrapPanel1.Children.Add(tb);
if (google)
{
col1_links.Add("http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=" + HttpUtility.UrlEncode(textBox1.Text));
}
else
{
col1_links.Add("http://yandex.ua/yandsearch?text=" + HttpUtility.UrlEncode(textBox1.Text));
}
col1_texts.Add(textBox1.Text);
webBrowser1.Navigate(col1_links[i].ToString());
i++;
SaveData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我收到以下错误:
答案 0 :(得分:0)
我可以用几行代码重写你的代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
var google = ((Button)sender).Content.ToString() == "Google";
var run1 = new Run(textBox1.Text);
var hl = new Hyperlink(run1)
{
NavigateUrl = google ? "http://www.google.com.ua/#hl=ru&source=hp&biw=1280&bih=933&q=" :
"http://yandex.ua/yandsearch?text=";
Tag = i;
};
var tb = new TextBlock();
hl.Click += new RoutedEventHandler(hl_Click);
tb.Inlines.Add(hl);
tb.Inlines.Add(" ");
wrapPanel1.Children.Add(tb);
col1_links.Add(hl.NavigateUrl + HttpUtility.UrlEncode(textBox1.Text));
col1_texts.Add(textBox1.Text);
webBrowser1.Navigate(col1_links[i].ToString());
i++;
SaveData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
尝试更专业;)。首先,NavigateUrl是“string”(而不是Uri),尝试使用它(不是NavigateUri)。第二件事是没有“i”变量,它在哪里?