C# 使用autosuggestbox的UWP中的无限循环或无限递归错误
//用于搜索查询的类C# 使用autosuggestbox的UWP中的无限循环或无限递归错误,c#,windows,xaml,uwp,C#,Windows,Xaml,Uwp,//用于搜索查询的类 public class SearchQueries { List<data> list = new List<data>(); string response; // The method that return the list after it is set public List<data> GetData() {
public class SearchQueries
{
List<data> list = new List<data>();
string response;
// The method that return the list after it is set
public List<data> GetData()
{
return list;
}
//问题从这里开始,当我在other中实例化这个类中的search类以获取查询的autosuggestbox中的文本值时,每当我尝试启动页面时,它就会崩溃。每当我输入地址默认数据(例如string address=“London”)时,它都可以正常工作,当我启动它时,页面会打开,每当我在autosuggestbox中输入时,它会给我与伦敦相关的结果
Search search = new Search();
string address = search.Address;
list.Clear();
//请注意,我使用的教程是从本地文件夹获取数据,但我尝试从GoogleAPI获取我的数据
string dataUri = "https://maps.googleapis.com/maps/api/place/autocomplete/json?key=AIzaSyDBazIiBn2tTmqcSpkH65Xq5doTSuOo22A&input=" + address;
string Api = System.Uri.EscapeDataString(dataUri);
HttpClient client = new HttpClient();
client.Timeout = TimeSpan.FromMilliseconds(1000);
try
{
response = await client.GetStringAsync(Api);
for (uint i = 0; i < jsonarray.Count; i++)
{
string json_string_object = jsonarray.GetObjectAt(i)["description"].ToString();
list.Add(new data() { name = json_string_object });
}
}
catch (TimeoutException e)
{
ContentDialog myDlg = new ContentDialog()
{
PrimaryButtonText = "OK"
};
myDlg.Content = e.ToString();
}
}
//自动建议框的文本更改方法
private async void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if (sender.Text.Length > 1)
{
var marchingData = queries.getmatchingCustomer(sender.Text);
sender.ItemsSource = marchingData.ToList();
}
else
{
sender.ItemsSource = new string[] { "No suggestion...." };
}
}
}
}
请共享一个可以复制您的问题的文件。您共享的代码格式不正确。从这段代码中很难找到问题所在。这段代码不能被最小化,因为它们都在一起工作。我在那里对所有内容都发表了评论,以便于解释。请让我知道不清楚的方面。我认为您需要重新格式化代码,因为您的评论拆分了您的代码,我认为您发布了许多难以理解的小片段。我编辑了一些部分,我不能不发表评论,您可以将其与类分开以便于理解
public IEnumerable<data> getmatchingCustomer(string query)
{
return list.Where(c => c.name.IndexOf(query, StringComparison.CurrentCultureIgnoreCase) > -1).OrderByDescending(c => c.name.StartsWith(query, StringComparison.CurrentCultureIgnoreCase));
}
// constructor for returning the SetData() method
public SearchQueries()
{
// It points to this method whenever the application crash, with the notification of infinite loop or infinite recursion
SetData();
}
}
public sealed partial class Search : Page
{
public string theaddress { get; set; }
SearchQueriess queries = new SearchQueriess();
public Search()
{
this.InitializeComponent();
myMap.Loaded += MyMap_Loaded;
theaddress = locationAddress.Text;
}
private async void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
if (sender.Text.Length > 1)
{
var marchingData = queries.getmatchingCustomer(sender.Text);
sender.ItemsSource = marchingData.ToList();
}
else
{
sender.ItemsSource = new string[] { "No suggestion...." };
}
}
}
}