【发布时间】:2013-11-19 11:37:43
【问题描述】:
我正在尝试在我的 Windows 应用程序中解析一些 json 数据。我已经为此编写了一些代码。代码没有错误,但我的文本块中没有数据。这是我的 XAML
<TextBlock Name="acc1" Margin="180, 60, 0, 0" Text="{Binding Accnumber1}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
<TextBlock Name="bal1" Margin="180, 90,0, 0" Text="{Binding Availablebalance}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
<TextBlock Name="acc2" Margin="180, 140, 0, 0" Text="{Binding Accnumber2}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
<TextBlock Name="bal2" Margin="180, 170, 0, 0" Text="{Binding Availablebalance}" Foreground="White" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" />
这是我的类文件
public MainPage()
{
InitializeComponent();
CheckForAnimation();
BackKeyPress += OnBackKeyPressed;
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += webClient_DownloadStringCompleted;
ProgressBarRequest.Visibility = System.Windows.Visibility.Visible;
webClient.DownloadStringAsync(new Uri("http://mobimybank.appspot.com/loginresponse.json"));
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
if (!string.IsNullOrEmpty(e.Result))
{
var root1 = JsonConvert.DeserializeObject<RootObject>(e.Result);
this.DataContext = root1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
ProgressBarRequest.Visibility = System.Windows.Visibility.Collapsed;
}
}
这是 RootObject 类文件
public class Mybank
{
public string status { get; set; }
}
public class Account
{
public string Accnumber1 { get; set; }
public string Availablebalance { get; set; }
public string Accnumber2 { get; set; }
}
public class Accounts
{
public List<Account> Account { get; set; }
}
public class Loginresponse
{
public Mybank { get; set; }
public Accounts { get; set; }
}
public class RootObject
{
public Loginresponse loginresponse { get; set; }
}
当我在数据上下文中添加观察者时,它告诉我数据已被获取。但是数据没有显示在上面给定的文本块中。 请告诉我我做错的地方或显示数据的正确方法。
【问题讨论】:
-
我会使用 DependencyProperties: Text ="{Binding RelativeSource={RelativeSource AncestorType={x:Type ClassReferingTo}, AncestorLevel=1}, Path=Brand_Name}" ..... 你的代码在后面:创建一个依赖对象(Brand_Name)来引用。你应该处理 depProps ...谷歌它:)
-
您已将 TextBloxks 绑定到属性 Brand、Type。但是,当您更改 ObservableCollection 时,会使用属性 Results 引发事件。
-
首先感谢您的回复,请您再简单介绍一下,以便我能准确理解我的错误。
-
你在结果中得到了什么?你拿到名单了吗?如果是的话,你在哪里分配文本块上的内容?
-
文本块字段为空。我在我的文本块中分配内容 - txt 7/8/9/10
标签: c# json windows-phone-7 windows-phone-8