Vb.net 如何将WinRT中的图像绑定到列表框
我是xaml编程新手。我一直在尝试将多个图像绑定到一个列表框,但运气不佳。我能在winrt应用程序中看到文本,但看不到图像。代码如下:Vb.net 如何将WinRT中的图像绑定到列表框,vb.net,xaml,windows-8,windows-runtime,Vb.net,Xaml,Windows 8,Windows Runtime,我是xaml编程新手。我一直在尝试将多个图像绑定到一个列表框,但运气不佳。我能在winrt应用程序中看到文本,但看不到图像。代码如下: Imports Windows.Storage.Pickers Imports Windows.Storage Public NotInheritable Class MainPage Inherits Page Dim p As System.Uri ''' <summary> ''' Invoked when
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
导入Windows.Storage.Pickers
导入Windows.Storage
公共不可继承类主页面
继承页面
dimpas System.Uri
'''
当此页面即将显示在框架中时调用“”。
'''
描述如何到达此页面的“”事件数据。参数
''属性通常用于配置页面。
受保护的覆盖子项OnNavigatedTo(e作为Navigation.NavigationEventArgs)
端接头
Private Async Sub-SelectFileName\u Click(发件人作为对象,e作为路由EventTargets)处理\u SelectFileName.Click
Dim SelectedFileNameObject作为新的FileOpenPicker
SelectedFileNameObject.FileTypeFilter.Add(“*”)
Dim SelectedFileName As IReadOnlyList(存储文件的)=等待SelectedFileNameObject.PickMultipleFileAsync
将a调暗为新的可观察集合(ImageLoc的)
对于i As Integer=0,选择SelectedFileName.Count-1
p=新Uri(SelectedFileName.Item(i).Path.ToString,UriKind.RelativeOrAbsolute)
a、 添加(带有{.ImageLocation=_SelectedFileName.Item(i).Path.ToString.LineFour=p}的新ImageLoc())
下一个
ListName.ItemsSource=a
端接头
末级
公共类ImageLoc
作为字符串的公共位置
属性ImageLocation()作为字符串
得到
返回位置
结束
设置(ByVal值作为字符串)
位置=值
端集
端属性
公共b作为Uri
公共属性LineFour()作为Uri
得到
返回b
结束
Set(ByVal值作为Uri)
b=值
端集
端属性
末级
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>
有什么想法吗?首先,你实际上不需要做正手的
位图图像。Image
控制将ImageSource
或URI
直接绑定到其Source
属性的间谍端口。另外,您确定要传递的URI的格式正确吗?有时,它们需要在前面加上程序包名称前缀才能正常工作。请参阅
基本上:
<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*);
位图图像bmImage;
公共位图图像BMImage
{
得到
{
返回bmImage;
}
}
bmImage=新位图图像();
bmImage.UriSource=新Uri(新Uri(
*您的文件路径*,
*您的图像名称*);
有关更多示例,请参阅我的博客。很抱歉,这是用C#编写的。我对VB不熟悉。更改为?