salwei35
salwei35

Reputation: 27

How to reference C# picture in Xaml

So I have hard coded in my picture in my C# But I want it to be placed on my XAML design.But I don't get the picture to show.

  MainPage.GetPicURL = "http://www.school.edu/images/logo-internal.png";
        Uri uriImg = new Uri(MainPage.GetPicURL);
        Windows.UI.Xaml.Media.Imaging.BitmapImage imgBitMap = new
        Windows.UI.Xaml.Media.Imaging.BitmapImage();
        imgBitMap.UriSource = uriImg;
        imgLogo.Source = imgBitMap;


<Image x:Name="image" HorizontalAlignment="Left" Height="100" Margin="10,16,0,0" VerticalAlignment="Top" Width="340" Source="imgLogo.Source"/>

Upvotes: 1

Views: 57

Answers (1)

cahyo
cahyo

Reputation: 607

You can set your image source from C# code and left xaml empty

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
  x:Class="Test.MyPage">
    <ContentPage.Content>
        <StackLayout>
            <Image 
            x:Name="myImage"
            HorizontalAlignment="Left"
            Height="100" Margin="10,16,0,0"
            VerticalAlignment="Top"
            Width="340"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

in your c# code

namespace Test
{
    public class MyPage : ContentPage
    {
        public MyPage()
        {
            InitializeComponent();
            Image img = this.myImage;
            img.Source = ImageSource.FromUri(new Uri("http://www.school.edu/images/logo-internal.png"));
        }
    }
}

Upvotes: 1

Related Questions