我在使用堆叠布局的内容页中遇到了一个展示图片的问题。我查看了 Xamarin API 文档并找到了 Xamarin.Forms.Image.Source 属性,但没有样例代码可以参考。我还检查了与我的文件名路径相匹配的 C# 代码,但在 Xamarin 中可能会稍有不同,因为这是第一次这么做。我目前正在通过 Visual Studio 2013 中的 Android 模拟器 (Google Nexus 5) 进行测试,一切正常,但图片无法显示。
图片来源:
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
}
var NfcImage = new Image{Aspect = Aspect.AspectFit}; NfcImage.Source = ImageSource.FromFile("xamarin_logo.png");
来指定图像在页面上的位置?如果我将其放置在new Image { }
构造函数中是不起作用的。 - TheAmazingKnightSource = "xamarin_logo.png"
,然后它就可以工作了。再次感谢您提供的链接,真的帮了我很大的忙。 - TheAmazingKnight