如何在Xaml中引用C#图片

时间:2017-02-20 05:42:13

标签: c# xaml

所以我在C#中对我的照片进行了硬编码但是我希望将它放在我的XAML设计上。但是我没有得到要显示的图片。

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

1 个答案:

答案 0 :(得分:1)

您可以从C#代码设置图像源,并将xaml设置为空

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

在您的c#代码中

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