繁体   English   中英

从WCF服务获取地址名称

[英]get address name from WCF service

我已经创建了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;
    }
}

首先,由于您是在Web服务源上控制json响应的结构,因此应修复其结构。

根据您的数据,搜索结果包含一个页面结果计数和一组结果,因此我更改了json以反映这一点。 这意味着可以使用合适的json解串器进行解析,并使您的代码紧凑,整洁。 确保在项目中设置了Newtonsoft json库。

C#类

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM