【发布时间】:2015-08-31 06:20:09
【问题描述】:
我无法在堆栈布局中的内容页面上显示图像。我查看了 Xamarin API 文档并找到了 Xamarin.Forms.Image.Source Property,但没有示例代码可以查看它是如何编写的。我还检查了它是如何用 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
}
【问题讨论】:
-
您是否阅读过此文档 - developer.xamarin.com/guides/cross-platform/xamarin-forms/…?通常在 Android 上,您将图像添加为可绘制资源,然后仅指定图像名称,Forms 会在资源中找到合适的图像。
-
感谢您的信息。我还有一个问题要问,我在哪里使用此代码
var NfcImage = new Image{Aspect = Aspect.AspectFit}; NfcImage.Source = ImageSource.FromFile("xamarin_logo.png");指定页面上的图像位置?如果我将它放在new Image { }构造函数中,则不起作用 -
我想通了,我按照标题为“本地图像”的那个,将文件路径名调整为
Source = "xamarin_logo.png",它工作了。再次非常感谢您的链接。真的很有帮助。
标签: c# xamarin xamarin.forms imagesource