我构建了一个天气应用程序,可以获取世界主要城市的天气信息。我使用 wsdl 以 xml 格式下载天气数据,然后使用 XmlDocument 类从元素中获取所需的数据。我在文本框中显示此信息。我将文本框的 HorizontalScrollBarVisibility 和 VerticalScrollBarVisibility 设置为自动,以便在文本太长时显示滚动条。我还实现了一个 _MouseLeftButtonDown 监听器,因为我已经从我的应用程序中删除了标题栏。但是每次显示滚动条并单击它时,都会调用 DragMove() 方法,因此我无法滚动文本。
我怎样才能防止这种情况发生?
这是我的代码,它仅适用于我从 wsdl 转换的 GlobalWeather 和 Country 类。如果您愿意,我可以发布代码,或者您可以下载这些类的 wsdl 并将其转换为 .cs ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
namespace WeatherApp
{
/// <summary>
/// Interaktionslogik für Window1.xaml
/// </summary>
public partial class Window1 : Window
{
// http://www.webservicex.net/globalweather.asmx?WSDL
private static GlobalWeather wetterObjekt;
//http://www.webservicex.net/ws/WSDetails.aspx?WSID=17&CATID=7
Country country;
private static string wetter;
private static string wetterAktuell;
private static List<string> countryList;
private static List<string> cityList;
public Window1 ()
{
InitializeComponent ();
wetterAktuell = "";
wetter = "";
wetterObjekt = new GlobalWeather ();
country = new Country ();
countryList = new List<string> ();
cityList = new List<string> ();
getCountries ();
string firstStr = "(keins ausgewählt)";
object firstItem = firstStr;
txtStadt.Items.Add(firstItem);
txtStadt.SelectedIndex = 0;
txtLand.SelectedIndex = 0;
txtWetter.IsEnabled = false;
txtLand.SelectionChanged += new SelectionChangedEventHandler ( txtLand_SelectionChanged );
}
private void txtLand_SelectionChanged ( object sender, SelectionChangedEventArgs e )
{
Console.WriteLine(cityList.Count);
if (cityList.Count > 0)
{
txtStadt.Items.Clear();
cityList.Clear();
string firstStr = "(keins ausgewählt)";
object firstItem = firstStr;
txtStadt.Items.Add(firstItem);
txtStadt.SelectedIndex = 0;
}
if ((string)(txtLand.SelectedValue) == "(keins ausgewählt)")
{
return;
}
else
{
string land = "";
land = txtLand.SelectedValue.ToString();
getStadt(land);
//Console.WriteLine(wetterObjekt.GetCitiesByCountry(land));
//Console.WriteLine(land);
}
}
private void getStadt (string land)
{
XmlDocument document = new XmlDocument();
document.LoadXml(wetterObjekt.GetCitiesByCountry(land));
XmlNodeList nl = document.GetElementsByTagName("NewDataSet");
for (int x = 0; x < nl[0].ChildNodes.Count; x++)
{
for (int y = 1; y < nl[0].ChildNodes[x].ChildNodes.Count; y += 2)
{
cityList.Add(nl[0].ChildNodes[x].ChildNodes[y].InnerText);
}
}
cityList.Sort();
for (int x = 0; x < cityList.Count; x++)
{
txtStadt.Items.Add(cityList[x]);
}
}
private void getCountries()
{
string countries = country.GetCountries ();
XmlDocument document = new XmlDocument ();
document.LoadXml ( countries );
XmlNodeList nl = document.GetElementsByTagName ( "NewDataSet" );
string firstStr = "(keins ausgewählt)";
object firstItem = firstStr;
txtLand.Items.Add(firstItem);
for ( int x = 0; x < nl [0].ChildNodes.Count; x++ )
{
for ( int y = 0; y < nl [0].ChildNodes [x].ChildNodes.Count; y++ )
{
countryList.Add ( nl [0].ChildNodes [x].ChildNodes [y].InnerText );
}
}
for ( int i = 0; i < countryList.Count; i++ )
{
txtLand.Items.Add ( countryList [i] );
}
}
private void getWeather ( string stadt, string land )
{
try
{
wetter = wetterObjekt.GetWeather(stadt, land);
xmlParsen(wetter);
txtWetter.Text = wetterAktuell;
}
catch (System.Xml.XmlException e)
{
clearWetterTextBox();
txtWetter.Text = "Keine Wetterdaten gefunden!";
//Console.WriteLine("Keine Wetterdaten gefunden!");
}
}
private void btnSchliessen_Click ( object sender, RoutedEventArgs e )
{
this.Close ();
}
private void xmlParsen (string wetter)
{
XmlDocument document = new XmlDocument ();
try
{
document.LoadXml(wetter);
XmlNodeList nl = document.GetElementsByTagName("CurrentWeather");
for (int i = 0; i < nl[0].ChildNodes.Count; i++)
{
wetterAktuell = wetterAktuell + (nl[0].ChildNodes[i].Name + ": " + nl[0].ChildNodes[i].InnerText) + "\n";
}
}
catch (System.Xml.XmlException e)
{
throw new System.Xml.XmlException();
}
}
private void btnGetWetter_Click ( object sender, RoutedEventArgs e )
{
clearWetterTextBox ();
getWeather (txtStadt.Text, txtLand.Text);
}
private void clearWetterTextBox ()
{
wetterAktuell = "";
this.txtWetter.Clear ();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
}
}