无法将类型“字符串"隐式转换为“Windows.UI.Xaml.Media.Imaging.BitmapImage" [英] Cannot implicitly convert type 'string' to 'Windows.UI.Xaml.Media.Imaging.BitmapImage

查看:25
本文介绍了无法将类型“字符串"隐式转换为“Windows.UI.Xaml.Media.Imaging.BitmapImage"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XAML 设计中显示存储在本地目录中的图像.

I am trying to show an image stored in local directory inside XAML design.

我有本地图片的路径.

ImagePage.xaml

<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>

ImagePage.xaml.cs

string strImgPath = "ms-appx:///Images/Music/localImg.png";
public string imgPath
{
     get { return strImgPath; }
}
imageClaz obj = new imageClaz();
obj.imgArt = imgPath;

imageClaz()

public class imageClaz
{
     public string imgArt { get; set; }
}

推荐答案

如果您的目标是 WinRT 应用程序,您应该使用 Windows.UI.Xaml.Media.Imaging.BitmapImage.

You should use Windows.UI.Xaml.Media.Imaging.BitmapImage if you are targeting WinRT app.

只需将 imgArt froms 字符串更改为 BitmapImage

Just change the imgArt froms string to BitmapImage

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));

这篇关于无法将类型“字符串"隐式转换为“Windows.UI.Xaml.Media.Imaging.BitmapImage"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆