This is my xaml:
x:Class="FinanciaLounge.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{d:DesignData SampleData/News_MainViewModelSampleData.xaml}"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
I want to bind the data in list1 to MainListbox, so that i can show the news.
i'm not able to bind the list1 to MainListBox....
m new to windows phone development, can anybody help???? and this is my xaml.cs code:
namespace FinanciaLounge
{
public partial class Page1 : PhoneApplicationPage
{
int a = 0;
string json = "";
RootObject newss { get; set; }
public Page1()
{
InitializeComponent();
DataContext = App.ViewModel2;
this.Loaded += new RoutedEventHandler(Page1_Loaded);
}
private void Page1_Loaded(object sender, RoutedEventArgs e)
{
if (!App.ViewModel2.IsDataLoaded)
{
App.ViewModel2.LoadData();
}
string url = @"http://www.financialounge.com/YUCOM/?action=latest_news";
HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
hWebRequest.Method = "GET";
hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
}
public void Response_Completed(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
json = streamReader.ReadToEnd();
newss = JsonConvert.DeserializeObject(json);
string date1;
date1 = newss.latest_news.extractionDate.ToString();
List- list1 = new List
- ();
foreach (var item in newss.latest_news.items)
{
string itmid = item.id;
string itmcon = item.content;
string itmpub = item.pubDate;
string itmtitle = item.title;
list1.Add(new Item(itmid,itmcon,itmpub,itmtitle));
}
MainListBox.DataContext = list1;
//MainListBox.ItemsSource = list1;
//MainListBox.DataContext = newss.latest_news;
}
}
public class Item
{
public string id { get; set; }
public string pubDate { get; set; }
public string title { get; set; }
public string content { get; set; }
public Item(string itmid, string itmcon, string itmpub, string itmtitle)
{
this.id = itmid;
this.content = itmcon;
this.pubDate = itmpub;
this.title = itmtitle;
}
}
public class LatestNews
{
public string extractionDate { get; set; }
public List- items { get; set; }
}
public class RootObject
{
public LatestNews latest_news { get; set; }
}
private void Image_Tap(object sender, GestureEventArgs e)
{
a = a + 1;
if (a % 2 != 0)
{
LayoutRoot.Width = 100;
LayoutRoot.HorizontalAlignment = HorizontalAlignment.Right;
}
else
{
LayoutRoot.Width = 478;
LayoutRoot.HorizontalAlignment = HorizontalAlignment.Left;
}
}
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
// Navigate to the new page
NavigationService.Navigate(new Uri("/News_single.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}
private void MainListBox_Loaded(object sender, RoutedEventArgs e)
{
}
}
}
I want to bind the data in list1 to MainListbox, so that i can show the news.
i'm not able to bind the list1 to MainListBox....
m new to windows phone development, can anybody help????
Replies
Know the answer? Post it — somebody with the same question will find it here.