我正在尝试通过实现 mvvm 模式将来自异步回调响应的 json 对象的数据集合绑定到 windows phone 中的列表框...我能够将数据集合获取到 observablecollectionm<> 对象,但随后无法将其绑定到 UI 列表框.xaml 页面中的控件。
以下是 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");
}
}
}
以下是视图模型的代码
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>