我有困難撫養的內容頁面上的圖像中的堆棧佈局。我查看了Xamarin API文檔,發現Xamarin.Forms.Image.Source Property,但沒有示例代碼來了解它是如何寫入的。我也檢查了它是如何用C#編寫的,似乎與我的代碼在文件名路徑方面相匹配,但在Xamarin中,它可能會稍微有點不同,因爲這是第一次這樣做。我目前正在通過Visual Studio 2013中的Android模擬器(Google Nexus 5)進行測試的代碼運行正常,但圖像未顯示。如何在Xamarin.Forms中正確使用圖像源屬性?
圖片來源:
new Image
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Source = "/Assets/xamarin_logo.png",
},
全碼:
public NFCPage()
{
StackLayout stackLayout = new StackLayout // instantiate a StackLayout object to layout its children
{
Spacing = 5, // amount of spae between each child element
//HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.FillAndExpand, // defines how the elements should be laid out; fill the entire width of the content to the screen
BackgroundColor = Color.Blue,
Children = // gets a list of child elements
{
new Label
{
TextColor = Color.White,
BackgroundColor = Color.Red,
XAlign = TextAlignment.Center, // set text alignment horizontally
Text = "Google",
},
new Label
{
Text = "Place your device directly at the symbol.",
XAlign = TextAlignment.Center,
TextColor = Color.White,
},
new Image
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Source = "/Assets/xamarin_logo.png",
},
new Button
{
Text = "QR Code",
TextColor = Color.White,
},
new Button
{
Text = "?",
TextColor = Color.White,
},
}
};
Content = stackLayout; // apply stackLayout to Content
}
你看了這個文檔 - http://developer.xamarin.com/guides /跨平臺/ xamarin表單/工作與/圖片/?通常在Android上,您將圖像添加爲可繪製資源,然後只指定圖像名稱,表單將在資源中找到適當的圖像。 – Jason
感謝您的信息。我有另一個問題要問,我在哪裏指定圖像放置到頁面上,代碼如下:var NfcImage = new Image {Aspect = Aspect.AspectFit}; NfcImage.Source = ImageSource.FromFile( 「xamarin_logo.png」);'?如果我把它放在'new Image {}'的構造函數 – TheAmazingKnight
中,我想不出來,我跟着名爲「Local Images」的那個,並將文件路徑名調整爲'Source =「xamarin_logo.png」'工作。非常感謝鏈接。它確實有幫助。 – TheAmazingKnight