Search code examples
c#xamlwindows-phone-8windows-phone-8.1

Cannot implicitly convert type 'string' to 'Windows.UI.Xaml.Media.Imaging.BitmapImage


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

I have the path to local image.

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

Solution

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

    Just change the imgArt froms string to BitmapImage

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

    and set the Image like this,

        string strImgPath = "ms-appx:///Images/Music/localImg.png";
        imageClaz obj = new imageClaz();
        obj.imgArt = new BitmapImage(new Uri(strImgPath, UriKind.Absolute));