At the moment I have a simple Twitter client, on which there is a Refresh button which is linked to a command that refreshes the timeline through list binding. At the moment I am achieving this by:
XAML:
<Grid x:Name="TimelineGrid">
<Button Content="Refresh Timeline" Name="RefreshTimeline" Command="{Binding RefreshCommand}" />
<ListView Name="SearchListBox" ItemsSource="{Binding Tweets}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="132">
<Image Source="{Binding ImageUrl}"
Height="73" Width="73"
VerticalAlignment="Top" Margin="5,10,8,0"/>
<StackPanel Width="auto">
<TextBlock Text="{Binding Name}"
Foreground="#222" FontSize="28" />
<TextBlock Text="{Binding Text}"
Foreground="#555" TextWrapping="Wrap" FontSize="24" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
ViewModel.cs:
class ViewModel : INotifyPropertyChanged
{
public List<Tweet> Tweets { get; set; }
readonly Page page;
public ViewModel(Page page)
{
this.page = page;
RefreshCommand = new TwitterCommand<object>(OnRefresh);
}
public TwitterCommand<object> RefreshCommand { get; set; }
void OnRefresh(object obj)
{
PinAuthorizer auth =
new PinAuthorizer
{
Credentials = new LocalDataCredentials()
};
if (auth == null || !auth.IsAuthorized)
{
page.Frame.Navigate(typeof(oAuth));
return;
}
var twitterCtx = new TwitterContext(auth);
var timelineResponse =
(from tweet in twitterCtx.Status
where tweet.Type == StatusType.Home && tweet.Count == 200
select tweet)
.ToList();
Tweets =
(from tweet in timelineResponse
select new Tweet
{
Name = tweet.User.Name,
Text = tweet.Text,
ImageUrl = tweet.User.ProfileImageUrl
})
.ToList();
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Tweets"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public List<User> Users { get; set; }
}
Now I want to populate this timeline when the MainPage.xaml.cs is loaded.
At the moment I have this for MainPage.xaml.cs:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DataContext = new ViewModel(this);
InitializeTimeline();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
void InitializeTimeline()
{
// Get oAuth tokens (They already exist for this example).
PinAuthorizer auth =
new PinAuthorizer
{
Credentials = new LocalDataCredentials()
};
var twitterCtx = new TwitterContext(auth);
//create list timelineResponse populated of last 200 statuses from users home timeline.
var timelineResponse =
(from tweet in twitterCtx.Status
where tweet.Type == StatusType.Home && tweet.Count == 200
select tweet)
.ToList();
//new list of Tweet (contains strings for Name, Text and ImageUrl)
List<Tweet> Tweets = new List<Tweet>();
//Populate list with the data collected from timeline response
Tweets =
(from tweet in timelineResponse
select new Tweet
{
Name = tweet.User.Name,
Text = tweet.Text,
ImageUrl = tweet.User.ProfileImageUrl
})
.ToList();
}
}
Now I’m thinking the best way to go about this is to loop through this List and assign the values to their respective list values in MainPage.xaml but currently I’m struggling on how to go about that.
Any help much appreciated 🙂
If I understand right, you just need the tweets to show up when you load the main page – and not just when the refresh button is clicked.
To do that, just call the ViewModel RefreshCommand manually from the MainViewModel like this:
This goes in place of the
InitializeTimelinecall.The refresh command will then load the tweets and update the list – which is bound to the ListView anyway.
You can then get rid of InitializeTimeline.