WPF图像源绑定不起作用

时间:2017-02-28 04:03:14

标签: c# wpf xaml linq-to-twitter

我有一个WPF应用程序,它使用LinqToTwitter作为ListBox的数据源。在DataTemplate内呈现的数据来自具有文本和图像的twitter源。

SocialFeedView.xaml代码

<DockPanel Grid.Row="0" Background="#1da1f2">
        <TextBlock Text="#BluesAllDay" VerticalAlignment="Bottom"  FontSize="20" FontWeight="DemiBold" 
                   Foreground="White" HorizontalAlignment="Center"/>
    </DockPanel>

    <StackPanel Grid.Row="1">
        <ListBox x:Name="TweetList" HorizontalContentAlignment="Stretch"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Tweets}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Image Source="{Binding Image}" Grid.Column="0" Stretch="UniformToFill"
                               Margin="3" Width="50" Height="50" />
                        <TextBlock Text="{Binding Text}" Grid.Column="1" TextWrapping="Wrap" FontSize="17"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox></StackPanel>

这是其视图模型的基础代码

private ObservableCollection<Tweet> _tweets;
    public ObservableCollection<Tweet> Tweets
    {
        get
        {
            if (_tweets == null)
            {
                _tweets = new ObservableCollection<Tweet>();
            }
            return _tweets;
        }
        set { Set(ref _tweets, value, x => x.Tweets); }
    }

    private SingleUserAuthorizer authorizer = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = "xxxxxxxx",
            ConsumerSecret = "xxxxxxxxxxx",
            AccessToken = "xxxxxxxxxx",
            AccessTokenSecret = "xxxxxxxxxx"
        }
    };

    private List<Status> currentTweets;

    public SocialFeedViewModel()
    {
        GetMostRecent200HomeTimeLine();

        if (Tweets != null) Tweets.Clear();
        currentTweets.ForEach(tweet => Tweets.Add(new Tweet { Text = tweet.Text, Image = new System.Uri(tweet.User.ProfileImageUrl, UriKind.Absolute) }));
    }

    private void GetMostRecent200HomeTimeLine()
    {
        var twitterContext = new TwitterContext(authorizer);

        var tweets = from tweet in twitterContext.Status
                     where tweet.Type == StatusType.Home && tweet.Count == 200
                     select tweet;

        if (currentTweets != null) currentTweets.Clear();

        currentTweets = tweets.ToList();
    }

    public void PullNew200HomeTimeLine()
    {
        GetMostRecent200HomeTimeLine();

        if (Tweets != null) Tweets.Clear();
        currentTweets.ForEach(tweet => Tweets.Add(new Tweet { Text = tweet.Text, Image = new System.Uri(tweet.User.ProfileImageUrl, UriKind.Absolute) }));
    }
}

public class Tweet
{
    public string Text { get; set; }
    public Uri Image { get; set; }
}

Tweet类型ObservableCollection的绑定机制正在发挥作用。但是,一个问题是<Image Source="{Binding Image}"/>组件没有显示来自推文的图像。类型为URI的图像类。

我坚持这一点,并且已经没有关于如何使其发挥作用的选项。

这是ListBox输出(红色框应显示从Tweets集合中检索到的ProfileImageUrl

enter image description here

我错过了什么吗?非常感谢任何帮助。

0 个答案:

没有答案