目前我有一个简单的 Twitter 客户端,上面有一个刷新按钮,该按钮链接到一个通过列表绑定刷新时间线的命令。目前我正在通过以下方式实现这一目标:
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; }
}
现在我想在加载 MainPage.xaml.cs 时填充此时间线。
目前我有这个 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();
}
}
现在我在想解决这个问题的最好方法是遍历这个 List 并将值分配给 MainPage.xaml 中它们各自的列表值,但目前我正在努力解决这个问题。
非常感谢任何帮助:)