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中的值,但目前我正在努力如何去解決這個問題。
任何幫助非常感謝:)
現在,如果您使用IoC容器,您可以在沒有任何代碼隱藏的情況下執行此操作 - 但只要沒有太多代碼隱藏,您擁有的分離級別通常就足夠了。瞄準純粹的MVVM解決方案有時是不值得的:) – Krishna
完美,謝謝! – BradStevenson