C# 无法隐式转换类型';字符串';至';Windows.UI.Xaml.Media.Imaging.BitmapImage
我试图在XAML设计中显示存储在本地目录中的图像 我有本地图像的路径 ImagePage.xamlC# 无法隐式转换类型';字符串';至';Windows.UI.Xaml.Media.Imaging.BitmapImage,c#,xaml,windows-phone-8,windows-phone-8.1,C#,Xaml,Windows Phone 8,Windows Phone 8.1,我试图在XAML设计中显示存储在本地目录中的图像 我有本地图像的路径 ImagePage.xaml <ScrollViewer> <ListView x:Name="ViewImage" SelectionMode="None" IsActiveView="True"> <ListView.ItemsPanel> <ItemsPanelTemplate> <W
<ScrollViewer>
<ListView x:Name="ViewImage" SelectionMode="None" IsActiveView="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="1" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid x:Name="imgGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<StackPanel>
<Grid Height="50" Width="50">
<Grid Margin="2" Background="red">
<Image Source="{Binding imgArt}" Stretch="UniformToFill"
Height="40" Width="40"/>
</Grid>
</Grid>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
imageClaz()
如果您的目标是WinRT应用程序,则应使用Windows.UI.Xaml.Media.Imaging.BitmapImage 只需将imgArt从字符串更改为位图图像
public class imageClaz
{
public BitmapImage imgArt { get; set; }
}
这样设置图像
string strImgPath = "ms-appx:///Images/Music/localImg.png";
imageClaz obj = new imageClaz();
obj.imgArt = new BitmapImage(new Uri(strImgPath, UriKind.Absolute));
如果您的目标是WinRT应用程序,则应使用Windows.UI.Xaml.Media.Imaging.BitmapImage 只需将imgArt从字符串更改为位图图像
public class imageClaz
{
public BitmapImage imgArt { get; set; }
}
这样设置图像
string strImgPath = "ms-appx:///Images/Music/localImg.png";
imageClaz obj = new imageClaz();
obj.imgArt = new BitmapImage(new Uri(strImgPath, UriKind.Absolute));
源的类型为ImageSource,而不是string。可以使用以下内容设置视图模型:
public class ImageClaz
{
public ImageClaz(Uri uri)
{
this.ImgArt = new BitmapImage(uri);
}
public ImageSource ImgArt { get; set; }
}
使用路径创建Uri的位置:
var imgeClass = new ImageClaz(new Uri("ms-appx:///Images/Music/localImg.png"));
源的类型为ImageSource,而不是string。可以使用以下内容设置视图模型:
public class ImageClaz
{
public ImageClaz(Uri uri)
{
this.ImgArt = new BitmapImage(uri);
}
public ImageSource ImgArt { get; set; }
}
使用路径创建Uri的位置:
var imgeClass = new ImageClaz(new Uri("ms-appx:///Images/Music/localImg.png"));
关于BitmapImage的两个答案仅部分正确。事实上,XAML有一个内置转换器,除非您提供了转换器,否则您肯定可以将字符串传递给它。您的代码中可能有几个问题:
- 您没有通知UI有关属性的更改-缺少INotifyPropertyChanged: 例如:
- 第二件事-我不确定您是否设置了图像或其父对象的DataContext:
public MainPage() { this.InitializeComponent(); DataContext = this; }
- 您没有通知UI有关属性的更改-缺少INotifyPropertyChanged: 例如:
- 第二件事-我不确定您是否设置了图像或其父对象的DataContext:
public MainPage() { this.InitializeComponent(); DataContext = this; }
您可以提供样品。对不起。这是个打字错误。更新@Grant Winney如果您只是在XAML中硬编码字符串,它会起作用吗?指示通常,绑定到
字符串
属性仍将调用隐式转换器。因此,失败向我表明字符串本身无法自动转换。您可能需要编写自定义转换器,或绑定到实际类型为Image
的属性。如果您去掉ms appx://
,它可能会工作。幕后/Images/Music/localImg.png
会自动转换为ImageSource
。但是我还没有试过。对不起。这是个打字错误。更新@Grant Winney如果您只是在XAML中硬编码字符串,它会起作用吗?指示通常,绑定到字符串
属性仍将调用隐式转换器。因此,失败向我表明字符串本身无法自动转换。您可能需要编写自定义转换器,或绑定到实际类型为Image
的属性。如果您去掉ms appx://
,它可能会工作。幕后/Images/Music/localImg.png
会自动转换为ImageSource
。但我还没有测试D。这也是半对的,默认字符串绑定只适用于项目中的图像资产,外部图像不起作用。@kirie你说的外部图像是什么意思?如果字符串是有效的Uri,那么它应该可以工作。你试过什么?你可以试一下这个在UWP中不起作用,但是这个@kirie当然不起作用-你不能像那样访问那个文件。请阅读更多有关在UWP中访问数据和文件的信息。这些不是标准的桌面应用程序。我很确定,只有在手机上、桌面上甚至文件选择器能够打开驱动器上几乎所有的文件/文件夹时,才可以访问数据。这也是半对的,默认字符串绑定只适用于项目中的图像资产,外部图像不起作用。@kirie外部图像是什么意思?如果字符串是有效的Uri,那么它应该可以工作。你试过什么?你可以试一下这个在UWP中不起作用,但是这个@kirie当然不起作用-你不能像那样访问那个文件。请阅读更多有关在UWP中访问数据和文件的信息。这些不是标准的桌面应用程序。我很确定,只有在手机上、桌面上,甚至文件选择器能够打开驱动器上几乎所有的文件/文件夹时,才能访问数据。