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");
}
}
}
以下は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>