2013-11-26 17 views
0

我正在使用Windows Phone 7應用程序在運行時下載幾個圖像(這意味着它們不會作爲資源添加到我的可執行文件中)。如何將我下載的圖像設置爲我的列表框UI對象?

對於下載圖像,我使用下一個鏈接描述的方法: Set image source to an uri 它似乎工作正常。

我很難嘗試將我的下載圖像設置到包含自定義控件的列表框中。

在我的XAML頁面我lisbox看起來是這樣的:

<ListBox x:Name="listBox_1" Margin="0,0,-12,0" ItemsSource="{Binding Items}"> 
      <ListBox.ItemTemplate> 
        <DataTemplate> 
         <mylocal:customcontrol_A id="{Binding PId}" /> 
        </DataTemplate> 
      </ListBox.ItemTemplate> 
      <ListBox.ItemsPanel> 
        <ItemsPanelTemplate> 
          <toolkit:WrapPanel Orientation="Horizontal" /> 
        </ItemsPanelTemplate> 
      </ListBox.ItemsPanel> 
    </ListBox>  

我的自定義控件包含一個圖像和幾個TextBlock中的如下:

<Grid x:Name="LayoutRoot" Height="180" Width="470" Margin="0,10,0,0"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="452*" /> 
     <ColumnDefinition Width="18*" /> 
    </Grid.ColumnDefinitions> 
    <Image Name="img_1" Source="{Binding PImage1, Converter={StaticResource requestConverter}}" /> 
    <TextBlock Name="txtB_0" Text="{Binding PName0}" Foreground="Black" /> 
    <TextBlock Name="txtB_1" Text="{Binding PName1}" Foreground="Black" /> 
</Grid> 

即時通訊使用的轉換器來啓動Web客戶端即會下載圖像。

一旦我的異步請求完成,並且我的圖像存儲在我的IsolatedStorageFile區域中,我可以在我的MainPage的回調函數上構建一個BitMapImage,但是我還沒有找到如何迭代我的listBox_1以訪問我的自定義命令圖像設置Source屬性如上面的鏈接建議。

我想做點什麼相當於未來

BitmapImage image = new BitmapImage(); 
image.SetSource(stream); 

foreach (customcontrol_A bc in listBox_1.Items) 
{ 
     if (bc.id == id) 
     { 
      bc.img_1.Source = image; 
      founded = true; 
      break; 
     } 
} 

但問題是,listBox_1.Items是myBindingClassA的實例,而不是實際的GUI元素customcontrol_A。

我真的很感激任何關於如何獲得對圖像GUI對象的引用的建議,所以我可以簡單地使用BitmapImage設置源。

非常感謝!

+0

我回答之前的一個簡單問題,爲什麼您需要將它存儲在本地?你知道一個事實,圖像永遠不會改變嗎? –

+0

嗨肖恩,是的,它不會改變,我在本地需要它的情況下,應用程序在沒有連接的地區使用,也只做一次,並保存數據消費用戶。 – Alx

回答

0

您可以輕鬆將延遲加載邏輯放在保存您的PImage1屬性的視圖模型中。你的財產需要返回一個ImageSource。

private ImageSource _source; 

public ImageSource PImage1 
{ 
    get 
    { 
     if (_source == null) 
     { 
      using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       if (storage.FileExists("Image.jpg")) 
       { 
        var bitmap = new BitmapImage(); 
        bitmap.SetSource(storage.OpenFile("Image.jpg", FileMode.Open)); 
        _source = bitmap; 
        return _source; 
       } 
      } 

      LoadImage(); 
     } 
     return _source; 
    } 
    set 
    { 
     _source = value; 
     OnPropertyChanged("PImage1"); 
    } 
} 

裏面的LoadImage方法,你會成爲一個請求,讓您的圖片,將其保存到本地存儲,然後設置PImage1屬性。確保在UI線程上設置屬性

private void LoadImage() 
{ 
    // make a request to get the image. 
    // Use your flavor of choice, WebRequest, HttpClient, WebClient 

    Stream image = GetImageFromMyFavoriteHttp; 
    using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     using (var file = storage.CreateFile("Image.jpg")) 
     { 
      image.CopyTo(file); 
     } 
    } 
    Deployment.Current.Dispatcher.BeginInvoke(() => 
    { 
     var bitmap = new BitmapImage(); 
     bitmap.SetSource(image); 
     Image = bitmap; 
    }); 
} 
+0

嗨肖恩,感謝您的迴應,所以,如果我理解正確的屬性請求從我的綁定類不會發生在GUI線程,對不對?或換句話說,這不會阻止應用程序,直到圖像請求下載完成,這是正確的嗎? – Alx

+0

請求屬性值確實發生在UI線程上,但對[web]服務的請求沒有(有點)。它將在UI線程上發出請求。 –

相關問題