我试图通过实现mvvm模式将来自json对象的数据集合从async回调响应绑定到windows phone中的列表框...我能够将数据集合到observablecollectionm<>对象但无法将其绑定到.xaml页面中的UI列表框控件。
以下是app.xaml中的代码
public static countryListViewModel countrylistVM { get; set; }
以下是countries.xaml页面中的代码。
public Countries()
{
InitializeComponent();
if (App.countrylistVM == null)
App.countrylistVM = new countryListViewModel();
DataContext = App.countrylistVM;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (!App.countrylistVM.IsDataLoaded)
{
App.countrylistVM.Loadcountries();
App.countrylistVM.IsDataLoaded = true;
}
}
以下是模型代码。
public class Model: INotifyPropertyChanged
{
private Countriesdata countries;
public Countriesdata Countries
{
get { return countries; }
set
{
if (countries != value)
{
countries = value;
RaisePropertyChanged("Countries");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propname)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
}
}
public class Countriesdata : INotifyPropertyChanged
{
private string countryid;
public string Countryid
{
get { return countryid; }
set
{
if (countryid != value)
{
countryid = value;
RaisePropertyChanged("Countryid");
}
}
}
private string countryname;
public string Countryname
{
get { return countryname; }
set
{
if (countryname != value)
{
countryname = value;
RaisePropertyChanged("Countryname");
}
}
}
以下是viewmodel的代码
public class countryListViewModel : INotifyPropertyChanged
{
HttpWebRequest crequest;
HttpWebResponse cresponse;
private bool isDataLoaded = false;
public bool IsDataLoaded
{
get { return isDataLoaded; }
set
{
if (isDataLoaded != value)
{
isDataLoaded = value;
RaisePropertyChanged("IsDataLoaded");
}
}
}
private Countriesdata countries;
public Countriesdata Countries
{
get { return countries; }
set
{
if (countries != value)
{
countries = value;
RaisePropertyChanged("Countries");
}
}
}
private ObservableCollection<Countriesdata> countrylist;
public ObservableCollection<Countriesdata> Countrylist
{
get { return countrylist; }
set
{
if (countrylist != value)
{
countrylist = value;
RaisePropertyChanged("Countrylist");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propname)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propname));
}
public void Loadcountries()
{
try
{
crequest = (HttpWebRequest)WebRequest.Create(serviceurls.getcountries);
crequest.Accept = "application/json";
IAsyncResult cresult = (IAsyncResult)crequest.BeginGetResponse(new AsyncCallback(Responsecallbackcountries), crequest);
}
catch (Exception e)
{
}
}
private void Responsecallbackcountries(IAsyncResult cresult)
{
try
{
string countryresult = string.Empty;
cresponse = (HttpWebResponse)crequest.EndGetResponse(cresult);
using (var Stream = cresponse.GetResponseStream())
{
using (var Reader = new StreamReader(Stream))
{
countryresult = Reader.ReadToEnd();
}
JObject Country = JObject.Parse(countryresult);
JArray Root = (JArray)Country["Countries"];
if (Root.Count != 0)
{
countrylist = new ObservableCollection<Countriesdata>();
var Dictionary = Root.ToDictionary(x => x, x => x);
JToken Tctry;
foreach (var cntry in Dictionary)
{
Tctry = cntry.Value;
countrylist.Add(
new Countriesdata
{
Countryid = Convert.ToString(Tctry["ID"]),
Countryname = Convert.ToString(Tctry["Name"])
});
}
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
Views.Countries vc = new Views.Countries();
});
}
}
}
catch (Exception e)
{
}
}
}
以下是.xaml页面的代码。
<ListBox x:Name="lstcountries"
Margin="0,0,-12,0"
ItemsSource="{Binding Countrylist}"
SelectedItem="{Binding Selectedcity, Mode=TwoWay}"
SelectionChanged="lstcountries_SelectionChanged" Background="white">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Background="Black">
<TextBlock Text="{Binding Countriesdata.Countryid}" Foreground="Black"
Visibility="Collapsed"/>
<TextBlock Text="{Binding Countriesdata.Countryname}" Foreground="Black"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:1)
您的数据模板是指模型,而不是列表框绑定到的集合中的项目。
相反,请尝试:
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Countryid}" Visibility="Collapsed"/>
<TextBlock Text="{Binding Countryname}" />
</StackPanel>
</DataTemplate>