繁体   English   中英

在Windows Phone 7应用程序的自动完成文本框中填充来自Web服务的数据

[英]Populate data from Web Service in autocomplete textbox in Windows phone 7 application

我正在为Windows Phone 7构建一个应用程序,在其中我需要在自动完成文本框中显示来自Web服务的数据。 Web服务中的字段名称为city_name。 因此,当用户输入字母“ A”时,它会显示Web服务中以A开头的城市名称。

我的xaml是:

 <toolkit:AutoCompleteBox x:Name="acBox" FilterMode="Custom" Width="290" 
    BorderThickness="0" Background="White" Canvas.Left="74" Canvas.Top="-3" Height="72">.
 <toolkit:AutoCompleteBox.ItemTemplate>
   <DataTemplate>
 <StackPanel Orientation="Horizontal">
   </StackPanel>
 </DataTemplate>
  </toolkit:AutoCompleteBox.ItemTemplate>
  </toolkit:AutoCompleteBox>

在我需要数据的页面中,我已经在调用Web服务并在列表框中显示数据。 请提供一些代码来帮助我,以在自动完成文本框中填充数据。

我的cs文件:

public class Cityy
    {
        public string city_name { get; set; }
        public string city_description { get; set; }
        public string city_image { get; set; }
        public BitmapImage ImageBind { get; set; }
        public string state { get; set; }

    }

    public const string CityyXml = "Cityy.xml";

    public City()
    {
        InitializeComponent();


      // this.acBox.ItemFilter = Search;
        LoadData();
    }



    private void LoadData()
    {
        bool isSuccess;
        //try to load data from iso store
        var doc = ReadXml(out isSuccess);
        if (isSuccess) PopulateList(doc);
        //if failed (data doesn't exists in iso store), download data from web service
        else
        {
            ServiceReference1.ServiceSoapClient client = new ServiceReference1.ServiceSoapClient();
            client.getCityListCompleted += new EventHandler<RahmService.getCityListCompletedEventArgs>(client_getCityListCompleted);
            client.getCityListAsync();

        }
    }

    void client_getCityListCompleted(object sender, ServiceReference1.getCityListCompletedEventArgs e)
    {
        var doc = XDocument.Parse(e.Result);
        PopulateList(doc);
        WriteXml(doc);
    }


    private void PopulateList(XDocument doc)
    {
        List<Cityy> listData = new List<Cityy>();

        foreach (var location in doc.Descendants("UserDetails"))
        {
            Cityy data = new Cityy();
            data.city_name = location.Element("city_name").Value;
            data.city_description = location.Element("city_description").Value;
            data.city_image = location.Element("city_image").Value;
            data.ImageBind = new BitmapImage(new Uri(@" http://...." +  data.city_image, UriKind.Absolute));
            data.state = location.Element("state").Value;
            listData.Add(data);
        }
        listBox1.ItemsSource = listData;
    }

    private XDocument ReadXml(out bool isSuccess)
    {
        isSuccess = false;
        var doc = new XDocument();
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            try
            {
                if (store.FileExists(CityyXml))
                {
                    using (var sr = new StreamReader(new IsolatedStorageFileStream(CityyXml, FileMode.OpenOrCreate, store)))
                    {
                        doc = XDocument.Load(sr);
                    }
                    isSuccess = true;
                }
            }
            catch (Exception ex) { }
        }
        return doc;
    }

    private bool WriteXml(XDocument document)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            try
            {
                using (var sw = new StreamWriter(new IsolatedStorageFileStream(CityyXml, FileMode.Create, store)))
                {
                    sw.Write(document.ToString());
                }
            }
            catch (Exception ex) { return false; }
        }
        return true;
    }

请帮助我在哪里编写代码。

就像ListBox一样,只需将数据源绑定到AutoCompleteBox并设置ItemFilter

XAML:

<toolkit:AutoCompleteBox x:Name="peopleBox" Height="70"/>

码:

this.peopleBox.ItemsSource = myDataSource;
this.peopleBox.ItemFilter += SearchCountry;

SearchCountry是:

bool SearchCountry(string search, object value)
        {
            if (value != null)
            {
                //return true if it contains the search key
                if (value.ToString().ToLower().IndexOf(search) >= 0)
                    return true;
            }
            // if not, return false
            return false;
        }

暂无
暂无

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

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