无法将类型“字符串"隐式转换为“Windows.UI.Xaml.Media.Imaging.BitmapImage"

编程入门 行业动态 更新时间:2024-10-26 22:16:11
本文介绍了无法将类型“字符串"隐式转换为“Windows.UI.Xaml.Media.Imaging.BitmapImage"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-25 13:39:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117639.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   类型   隐式   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!