2012-09-11 26 views
0

目前我有一個簡單的Twitter客戶端,其上有一個Refresh按鈕,該按鈕鏈接到通過列表綁定刷新時間軸的命令。在應用程序啓動時以編程方式在C#中設置XAML數據綁定

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(); 
    } 
} 

現在我正在考慮去了解這一點的最好辦法是遍歷名單和分配值各自的列表MainPage.xaml中的值,但目前我正在努力如何去解決這個問題。

任何幫助非常感謝:)

回答

1

如果我理解正確的,你只需要在微博露面時加載主頁 - 而不是在單擊刷新按鈕就在。

爲了做到這一點,剛剛從MainViewModel這樣手動調用視圖模型RefreshCommand:

((ViewModel)DataContext).RefreshCommand.Execute(null); 

這正好到位InitializeTimeline通話。

然後刷新命令將加載推文並更新列表 - 無論如何,該列表都綁定到ListView。

然後,您可以擺脫InitializeTimeline。

+0

現在,如果您使用IoC容器,您可以在沒有任何代碼隱藏的情況下執行此操作 - 但只要沒有太多代碼隱藏,您擁有的分離級別通常就足夠了。瞄準純粹的MVVM解決方案有時是不值得的:) – Krishna

+0

完美,謝謝! – BradStevenson

相關問題