Search code examples
c#visual-studioxamlwindows-store-apps

why do it show a black screen when i use Uri class?


I'm creating a simple windows store app in c#. When i'm binding my image control to display an image from code- behind, my screen goes black. Anyone know how i can solve the problem?

ImageService Class

public class ImageService 
{     

  public Image Image { get; set; }      

    public ImageService()
    {
        var uri = new System.Uri("ms-appx:///assets/Logo.scale-100.png");
        var bmp = new BitmapImage(uri);
        Image.Source = bmp;
    }
}

XAML file

  <Image x:Name="image" HorizontalAlignment="Left" Height="223"     Margin="394,279,0,0" VerticalAlignment="Top" Width="305" Source="{Binding Image, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Stretch="UniformToFill"/>

Solution

  • Use this:

    public class ImageService
    {
        public Uri Image { get; set; }
        public ImageService()
        {
            Image = new Uri("ms-appx:///assets/Logo.scale-100.png");
        }
    }
    

    The Source property of an Image is of type ImageSource which can be easily replaced by a Uri. (MSDN).