我正在开发wp8应用程序。我想创建AutoCompleteBox进行快速搜索。 这是我的xaml:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="ContentPanel" Grid.Row="0" Grid.RowSpan="2">
<maps:Map x:Name="MapWithMyLocation" Tap="mapWithMyLocation_Tap">
</maps:Map>
</Grid>
<Grid Grid.Row="0">
<controls:AutoCompleteBox x:Name="TxtSearch" TextChanged="TxtSearch_OnTextChanged" />
</Grid>
</Grid>
这是我在AutoCompleteBox上的事件TxtSearch_OnTextChanged
private void TxtSearch_OnTextChanged(object sender, RoutedEventArgs e)
{
if (TxtSearch.Text.Length > 0)
{
Maps_GeoCoding(TxtSearch.Text);
}
}
private async void Maps_GeoCoding(string sender)
{
var myGeolocator = new Geolocator();
var myGeoposition = await myGeolocator.GetGeopositionAsync();
var myGeocoordinate = myGeoposition.Coordinate;
MyGeoCoordinate =
CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
if (MyGeoCoordinate == null) return;
var geoQuery = new GeocodeQuery { SearchTerm = sender, GeoCoordinate = MyGeoCoordinate };
var locations = await geoQuery.GetMapLocationsAsync();
var items = new List<String>();
var str = "";
foreach (var item in locations)
{
str += item.Information.Address.City + " ";
str += item.Information.Address.Street + " ";
str += item.Information.Address.HouseNumber + " ";
items.Add(str);
str = "";
}
TxtSearch.ItemsSource = items;
}
但AutoCompleteBox显示一些错误的信息或不显示。有什么想法吗?
答案 0 :(得分:0)
刚刚改写了我的功能。现在我只使用FirstOrDefault获取一个项目。这是一个例子:
private async void Maps_GeoCoding(string sender)
{
var myAddress = new List<String>();
var myGeolocator = new Geolocator();
var myGeoposition = await myGeolocator.GetGeopositionAsync();
var myGeocoordinate = myGeoposition.Coordinate;
MyGeoCoordinate =
CoordinateConverter.ConvertGeocoordinate(myGeocoordinate);
if (MyGeoCoordinate == null) return;
var geoQuery = new GeocodeQuery { SearchTerm = sender, GeoCoordinate = MyGeoCoordinate };
var locations = await geoQuery.GetMapLocationsAsync();
var location = locations.FirstOrDefault();
if (location != null)
{
myAddress.Add(location.Information.Address.City + " " + location.Information.Address.Street + " " + location.Information.Address.HouseNumber);
MapWithMyLocation.Center = location.GeoCoordinate;
}
TxtSearch.ItemsSource = myAddress;
}
XAML与问题相同。