簡體   English   中英

C#Windows Phone中使用反序列化的JSON數據的數據綁定圖像鏈接

[英]data binding image link using deserialized json data in c# windows phone

我有一些json數據,所以我反序列化了該數據並將其綁定到列表框以在文本塊中顯示數據。...現在的問題是我正在嘗試在同一列表框中添加圖像並希望綁定圖像源使用反序列化對象的數據

我的意思是圖像的鏈接包含反序列化數據中存在的一些文本,這些文本將隨着數據的變化而變化。

誰能告訴我如何使用反序列化對象的數據,然后創建新字符串以傳遞圖像源,以將其綁定到列表框中的圖像?

提前致謝...

更新:

這是XAML的列表框部分

<ListBox x:Name="listdata" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding data}" Height="0" Margin="0,72,0,0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" DataContext="{Binding}">
                        <Image Grid.Row="1" Height="50" HorizontalAlignment="Left" Name="image1" Stretch="None" VerticalAlignment="Top" Width="50" Source="{Binding PicLink}" />
                        <TextBlock Height="50" HorizontalAlignment="Left" Margin="12,6,0,0" Name="textBlock1" Text="{Binding name}" VerticalAlignment="Top" Width="Auto" TextAlignment="Center" TextWrapping="Wrap" />
                        <TextBlock Height="Auto" HorizontalAlignment="Left" Margin="6,42,0,0" Name="textBlock3" Text="{Binding timedata}" VerticalAlignment="Top" Width="Auto" MinHeight="0" TextWrapping="Wrap" />
                    </Grid>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

這是C#代碼的一部分:

public class Datum
    {
        public string timedata { get; set; }
        string part = string.Empty;
        public string parts { get { return part; } set { part = value; } }
        public string name { get; set; }
        public string PicLink { get { return string.Format("google.com/{0}/image",part); } }
    }

一切正常,除了圖像.....圖像部分在我替換PicLink以返回靜態url而不是變量one時也起作用

如果要顯示來自網絡的圖像,則必須先下載它們。 對於列表來說這可能是個問題,因為它很耗時。

看一下這個問題,看看如何完成: 如何在wp7中顯示來自網絡的圖像?

如果可以將圖像包含在包中或以某種方式將其緩存在隔離的存儲中會更好!

更新:

您必須在類中添加“ BitmapImage”類型的屬性。 (也許byte []也可以工作,現在不知道,請嘗試一下)比起您可以將DataTemplate中的Image的ImageSource綁定到此屬性。 如果您將此類用於json序列化程序,則不要緊,因為序列化程序將在反序列化時忽略它。 為了更安全,請不要使用“ DataMember”屬性標記ist。 它可能看起來像:

private string _urlString;
[DataMember]
public string UrlPart
{
    get { return _urlString;}
    set 
    {
        _urlPart = value;
        LoadImage(); // in this function you do the downloading stuff
    }
}

private BitmapImage _itemImage;
public BitmapImage ItemImage
{
    get {return _itemImage;}
    // you can also add a setter if needed
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM