在我当前使用的客户端(而不是控制)中,接收/解析请求的方式是:
var result = (IDictionary<string, object>)e.GetResultData();
string id = result["id"].ToString();
string name = result["name"].ToString();
Dispatcher.BeginInvoke(() =>
{
id.ItemsSource = new List<String> {
id,
name};
}
XAML 看起来像:
<ListBox Height="168" HorizontalAlignment="Left" Margin="204,21,0,0" Name="id" VerticalAlignment="Top" Width="239" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="2">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
新更新 - 寻求帮助(未解决): 我正在尝试使用这些字符串,以便我可以将每个项目放入 ListBox(如果存在更多项目),而不是只抓取 1 个项目集(例如 ID、名称、链接)。
public class Datum
{
public string id { get; set; }
public string name { get; set; }
public string link { get; set; }
}
任何帮助都将不胜感激!
最佳答案
您需要将 ItemsSource
设置为实现 IEnumerable
的内容。
id.ItemsSource = new List<String> { id };
您还需要调整绑定(bind)以接收 DataContext
,因为您没有传入对象。
<TextBlock Text="{Binding}" />
如果您想在简单的 String 之外扩展,请创建一个类来包装您的内容,然后绑定(bind)到给定的属性。
class Person
{
String id {get; set;}
String name {get; set;}
}
<ListBox Height="168" HorizontalAlignment="Left" Margin="204,21,0,0" Name="id" VerticalAlignment="Top" Width="239" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="2">
<TextBlock Text="{Binding id}" />
<TextBlock Text="{Binding name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后在您后面的代码中,您可以像以前一样创建一个实例并进行分配。
Person p = new Person();
p.id = "id";
p.name = "name";
List<Person> people = new List<Person>();
people.Add(p);
id.ItemsSource = people;
这是一个简单的示例,但应该可以满足您的需求。
关于c# - 将来自 Facebook API 的字符串放入列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10607738/