我创建了一个WCF服务并在http://kailun92wcf.cloudapp.net/Service1.svc中发布到云中。如何使用getSearchCoords的方法获取所有名称并将其显示在列表中?测试结果的一个示例如下所示。
"{\"SearchResults\":[{\"PageCount\":\"1\"},{\"SEARCHVAL\":\"ORCHARD 22\",\"CATEGORY\":\"Buildin" +
"g\",\"X\":\"29483.4267\",\"Y\":\"31269.938\"},{\"SEARCHVAL\":\"ORCHARD BEL AIR\",\"CATEGORY\":\"" +
"Building\",\"X\":\"27071.2616\",\"Y\":\"31629.2465\"},{\"SEARCHVAL\":\"ORCHARD BOULEVARD\",\"C" +
"ATEGORY\":\"CATC\",\"X\":\"27614.8046\",\"Y\":\"31857.4392\"},{\"SEARCHVAL\":\"ORCHARD BUILDIN" +
"G\",\"CATEGORY\":\"Building\",\"X\":\"28449.6799\",\"Y\":\"31527.587\"},{\"SEARCHVAL\":\"ORCHARD" +
" BUILDING (FRIENDLY BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"28448.5715\",\"Y\":\"315" +
"26.146\"},{\"SEARCHVAL\":\"ORCHARD BUILDING (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreat" +
"ion\",\"X\":\"28448.3426\",\"Y\":\"31525.9693\"},{\"SEARCHVAL\":\"ORCHARD CENTRAL\",\"CATEGORY" +
"\":\"Building\",\"X\":\"28709.3453\",\"Y\":\"31452.9157\"},{\"SEARCHVAL\":\"ORCHARD CENTRAL (F" +
"RIENDLY BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"28709.3453\",\"Y\":\"31452.9157\"},{\"" +
"SEARCHVAL\":\"ORCHARD CENTRAL (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreation\",\"X\":\"28" +
"709.3453\",\"Y\":\"31452.9156\"},{\"SEARCHVAL\":\"ORCHARD CINELEISURE (WIRELESS HOTSPOTS" +
")\",\"CATEGORY\":\"Recreation\",\"X\":\"28347.9192\",\"Y\":\"31538.4923\"},{\"SEARCHVAL\":\"ORCH" +
"ARD COURT\",\"CATEGORY\":\"Building\",\"X\":\"28931.3725\",\"Y\":\"31225.6489\"},{\"SEARCHVAL\"" +
":\"ORCHARD CREDIT AUTO HOUSE\",\"CATEGORY\":\"Building\",\"X\":\"23255.1398\",\"Y\":\"35016.5" +
"269\"},{\"SEARCHVAL\":\"ORCHARD EMERALD (GREEN MARK BUILDINGS)\",\"CATEGORY\":\"Environm" +
"ent\",\"X\":\"28617.7255\",\"Y\":\"31549.8898\"},{\"SEARCHVAL\":\"ORCHARD FOUNTAIN CORNER\",\"" +
"CATEGORY\":\"Building\",\"X\":\"28464.8743\",\"Y\":\"31580.3349\"},{\"SEARCHVAL\":\"ORCHARD GA" +
"TEWAY\",\"CATEGORY\":\"Building\",\"X\":\"28666.655\",\"Y\":\"31427.7293\"},{\"SEARCHVAL\":\"ORC" +
"HARD GATEWAY @ EMERALD\",\"CATEGORY\":\"Building\",\"X\":\"28617.699\",\"Y\":\"31549.9633\"}," +
"{\"SEARCHVAL\":\"ORCHARD GRAND COURT PTE LTD (FRIENDLY BUILDINGS)\",\"CATEGORY\":\"Comm" +
"unity\",\"X\":\"28580.4218\",\"Y\":\"31071.6324\"},{\"SEARCHVAL\":\"ORCHARD HOTEL (FRIENDLY " +
"BUILDINGS)\",\"CATEGORY\":\"Community\",\"X\":\"27469.037\",\"Y\":\"32216.2037\"},{\"SEARCHVAL" +
"\":\"ORCHARD HOTEL (WIRELESS HOTSPOTS)\",\"CATEGORY\":\"Recreation\",\"X\":\"27469.0369\",\"" +
"Y\":\"32216.2037\"},{\"SEARCHVAL\":\"ORCHARD HOTEL GALLERIA\",\"CATEGORY\":\"Building\",\"X\"" +
":\"27494.5279\",\"Y\":\"32195.9069\"}]}"
我必须使用上面的结果将所有名称显示在windows phone 7.1的列表框中。我怎么能这样做?
我从其他来源看到他们将此名称显示在Windows Phone 7.1的列表框中:
private void Search_Click(object sender, RoutedEventArgs e)
{
//retrieving the results for the keywords the user input
searchError.Text = "Loading... Please Wait";
if (Classes.Global.searched == 1)
{
searchVal = new List<string>();
}
MobileClinicWebService.MCWebServiceSoapClient obj = new MobileClinicApp.MobileClinicWebService.MCWebServiceSoapClient();
obj.getSearchCoordsAsync(searchBar.Text.ToString());
obj.getSearchCoordsCompleted += new EventHandler<MobileClinicWebService.getSearchCoordsCompletedEventArgs>(obj_getSearchCoordsCompleted);
}
void obj_getSearchCoordsCompleted(object sender, MobileClinicWebService.getSearchCoordsCompletedEventArgs e)
{
//retrieving the results, and displaying it on the phone
string[] search = null;
if (!e.Result.ToString().Equals("error"))
{
using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(e.Result)))
{
while (jsonReader.Read())
{
if ((string)jsonReader.Value == "SEARCHVAL")
{
jsonReader.Read();
searchVal.Add((string)jsonReader.Value);
}
if ((string)jsonReader.Value == "X")
{
jsonReader.Read();
posx.Add(Double.Parse(jsonReader.Value.ToString()));
}
if ((string)jsonReader.Value == "Y")
{
jsonReader.Read();
posy.Add(Double.Parse(jsonReader.Value.ToString()));
}
}
}
search = new string[searchVal.Count];
for (int i = 0; i < searchVal.Count; i++)
{
search[i] = searchVal[i];
}
}
else
{
searchError.Text = "No Results Found";
}
if (search != null)
{
Classes.Global.searched = 1;
searchError.Text = "Search Results";
Results.ItemsSource = search;
}
}
答案 0 :(得分:1)
首先,由于您在Web服务源上控制json响应的结构,因此应该修复其结构。
根据您的数据,搜索结果包含页面结果计数和一组结果,因此我更改了json以反映这一点。这意味着可以使用合适的json反序列化器进行解析,并使您的代码紧凑而整洁。确保在项目中设置了Newtonsoft json库。
C#Classes
public class ResultSetPager<T>
{
public int PageCount { get; set; }
public IEnumerable<T> SearchResults { get; set; }
}
public class Place
{
public string SearchVal { get; set; }
public string Category { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
点击活动
private void LoadJsonData(object sender, RoutedEventArgs e)
{
string data = @"{
""PageCount"" : ""1"",
""SearchResults"": [
{
""SEARCHVAL"": ""ORCHARD22"",
""CATEGORY"": ""Building"",
""X"": ""29483.4267"",
""Y"": ""31269.938""
},
{
""SEARCHVAL"": ""ORCHARDBELAIR"",
""CATEGORY"": ""Building"",
""X"": ""27071.2616"",
""Y"": ""31629.2465""
}
]
}";
var pagedResults = JsonConvert.DeserializeObject<ResultSetPager<Place>>(data);
lstPlaces.ItemsSource = pagedResults.SearchResults;
}
的Xaml
<Grid>
<StackPanel>
<Button Click="LoadJsonData" Content="Test"></Button>
<ListBox x:Name="lstPlaces">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0 0 0 15">
<TextBlock Text="{Binding SearchVal}" FontSize="{StaticResource PhoneFontSizeLarge}" />
<TextBlock Text="{Binding Category}" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>