2017-02-20 26 views
1

所以我在我的C#圖片中進行了硬編碼,但是我希望將它放在我的XAML設計上。但是我沒有看到圖片。如何引用Xaml中的C#圖片

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

您可以設置從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")); 
     } 
    } 
}