【发布时间】:2019-07-29 09:56:51
【问题描述】:
我正在使用 SearchBar,我可以返回一些 API JSON 数据来查找公司名称及其股票代码。这可以很好地反序列化为 ObservableCollection 并显示在 ListView 中。
但是,有时结果中包含一个带有“-”(破折号)的股票代码。我想以某种方式测试我的 ObservableCollection 中的每个项目,并防止任何在符号名称中包含“-”的项目,然后将其排除在绑定到 ListView 的集合中。
这是我的搜索对象:
public class SearchObject
{
public string symbol { get; set; }
public string securityName { get; set; }
public string securityType { get; set; }
public string region { get; set; }
public string exchange { get; set; }
}
这是我的 JSON API 代码:
vSearchSymbol = mySearchBar.Text;
SearchUrl = string.Format("MY SEARCH API URL {0}", vSearchSymbol);
// Activity indicator visibility ON
activity_indicator.IsRunning = true;
// Getting JSON data from the Web
var content = await _client.GetStringAsync(SearchUrl);
// Deserialize the JSON data from content
var tr = JsonConvert.DeserializeObject<List<SearchObject>>(content);
// After deserializing, we store our data in an ObservableCollection List called 'trends'
ObservableCollection<SearchObject> trends = new ObservableCollection<SearchObject>(tr);
// Bind the list to the ListView
myList.ItemsSource = trends;
// Check the number of Items in the Observable Collection
int i = trends.Count;
if (i > 0)
{
// If the list count is > 0 then stop activity indicator
activity_indicator.IsRunning = false;
}
从 ObservableCollection 中删除符号包含“-”的任何 SearchObject 项目的最佳方法是什么?换句话说,我不希望任何带有包含破折号的符号的项目显示在 ListView 中。
非常感谢任何有关正确语法的指导。
【问题讨论】:
-
您可能还在寻找收藏视图。一种围绕可观察集合绑定视图的方法。这样,随着数据动态变化,视图始终保持同步。创建可观察集合。使用过滤器逻辑围绕它创建一个视图。然后将视图绑定到数据源。但是,如果@AnuViswan 的答案符合您的需要,它会更好/更薄。 jacobmsaylor.com/…