2012-10-04 25 views
0

我是xaml編程的新手。我一直在試圖將多個圖像綁定到一個沒有運氣的列表框。我能夠看到文本,但不能在winrt應用程序中的圖像。下面是代碼:如何將WinRT中的圖像綁定到列表框

Imports Windows.Storage.Pickers 
Imports Windows.Storage 

Public NotInheritable Class MainPage 
    Inherits Page 

    Dim p As System.Uri 

    ''' <summary> 
    ''' Invoked when this page is about to be displayed in a Frame. 
    ''' </summary> 
    ''' <param name="e">Event data that describes how this page was reached. The Parameter 
    ''' property is typically used to configure the page.</param> 
    Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs) 

    End Sub 

Private Async Sub SelectFileName_Click(sender As Object, e As RoutedEventArgs) Handles _SelectFileName.Click 

     Dim SelectedFileNameObject As New FileOpenPicker 
     SelectedFileNameObject.FileTypeFilter.Add("*") 

Dim SelectedFileName As IReadOnlyList(Of StorageFile) = Await SelectedFileNameObject.PickMultipleFilesAsync 

     Dim a As New ObservableCollection(Of ImageLoc) 
     For i As Integer = 0 To SelectedFileName.Count - 1 

      p = New Uri(SelectedFileName.Item(i).Path.ToString, UriKind.RelativeOrAbsolute) 

      a.Add(New ImageLoc() With {.ImageLocation = _SelectedFileName.Item(i).Path.ToString, .LineFour = p}) 

     Next 
     ListName.ItemsSource = a 

    End Sub 

End class 

Public Class ImageLoc 
    Public location As String 

    Property ImageLocation() As String 
     Get 
      Return location 
     End Get 
     Set(ByVal value As String) 
      location = value 
     End Set 
    End Property 
    Public b As Uri 
    Public Property LineFour() As Uri 
     Get 
      Return b 

     End Get 
     Set(ByVal value As Uri) 
      b = value 
     End Set 
    End Property 
End Class 

的XAML是:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel Height="auto" Width="auto" Orientation="Horizontal"> 
     <Button x:Name="SelectFileName" Width="100" Height="50" Content="Browse Files" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,40,0"/> 
     <ListBox x:Name="ListName" Width="700" Height="auto"> 
      <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical"> 
         <TextBlock Text="{Binding ImageLocation}" Height="auto" Width="auto"/> 
         <Image Height="100" Width="100"> 
          <Image.Source> 
           <BitmapImage UriSource="{Binding Path=LineFour}"/> 
          </Image.Source> 
         </Image> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 
</Grid> 

任何想法?

+0

變化? – Schuere

回答

0

首先,你實際上沒有做手寫BitmapImage代碼。控制間諜端口ImageImageSourceURI直接綁定到其Source屬性。另外,你確定你傳遞的Uris格式是否正確?有時候,他們需要在前面加上包名稱前綴以使它們正常工作。

0

change image source programmatically

基本上是:

<Image Margin="5" Source="{Binding BMImage}" Height="100"/> 

BitmapImage bmImage; 
public BitmapImage BMImage 
{ 
    get 
    { 
     return bmImage; 
    } 
} 

bmImage = new BitmapImage(); 
bmImage.UriSource = new Uri(new Uri(
    *your file path*, 
    *your image name*); 

更多的例子,看看我的博客here

+0

對不起,這是用C#編寫的。我不熟悉VB。 – mydogisbox