【问题标题】:embedded image in Xamarin Forms using c# [duplicate]使用 C# 在 Xamarin Forms 中嵌入图像 [重复]
【发布时间】:2017-12-31 01:44:04
【问题描述】:

伙计们,我在尝试将图像作为特定页面的背景时遇到了一些麻烦。我试图通过这里提到的嵌入式方法来做到这一点:https://developer.xamarin.com/guides/xamarin-forms/user-interface/images/#Embedded_Images .由于某种原因,当我运行该应用程序时,它崩溃了。我认为'BackgroundImage ='行出了点问题,因为当我这样做时我有点猜到了。当我注释掉该行时,应用程序加载正常,只是没有背景图像。我使用 build action = content 并作为设置复制到输出目录...

public class LoginPage : ContentPage
{
    Entry emailBox = new Entry();
    Entry passwordBox = new Entry();
    Button createAccount = new Button();
    Button forgotPassword = new Button();
    StackLayout layout = new StackLayout();
    Image embeddedImage = new Image { Source = ImageSource.FromResource("Charity.Properties.hands-426x640.jpg") };

    public LoginPage()
    {
        Title = "Login";
        BackgroundColor = Color.LightPink;
        BackgroundImage = "Charity.Properties.hands-426x640.jpg";

        emailBox.Placeholder = "email";
        emailBox.BackgroundColor = Color.LightPink;
        emailBox.PlaceholderColor = Color.HotPink;

        passwordBox.Placeholder = "password";
        passwordBox.BackgroundColor = Color.LightPink;
        passwordBox.PlaceholderColor = Color.HotPink;
        passwordBox.IsPassword = true;

        createAccount.Text = "create an account";
        createAccount.TextColor = Color.HotPink;
        createAccount.Font = Font.SystemFontOfSize(NamedSize.Medium);
        createAccount.BorderWidth = 0;
        createAccount.HorizontalOptions = LayoutOptions.Center;
        createAccount.VerticalOptions = LayoutOptions.CenterAndExpand;

        forgotPassword.Text = "recover password";
        forgotPassword.TextColor = Color.HotPink;
        forgotPassword.Font = Font.SystemFontOfSize(NamedSize.Medium);
        forgotPassword.BorderWidth = 0;
        forgotPassword.HorizontalOptions = LayoutOptions.Center;
        forgotPassword.VerticalOptions = LayoutOptions.CenterAndExpand;

        layout.VerticalOptions = LayoutOptions.End;
        layout.BackgroundColor = Color.LightPink;

        layout.Children.Add(emailBox);
        layout.Children.Add(passwordBox);
        layout.Children.Add(createAccount);
        layout.Children.Add(forgotPassword);

        Content = layout;
    }
}

【问题讨论】:

  • 查看您用于嵌入图像的路径,然后查看您用于背景的路径。提示提示。
  • @Nkosi 我试着把它改成这个 BackgroundImage = "Charity.Properties.hands-426x640.jpg";但是应用程序在运行时仍然崩溃。该链接在 xaml 中,对吗?我还没学过xaml,到目前为止我只学过c#..
  • 在 Droid 项目中,我将图像的构建操作设置为 AndroidResource。

标签: c# xamarin.forms


【解决方案1】:

这很有效,经过多次试验和错误,我只需要将图像添加到 .Droid(drawable) 和 .iOS(resources) 文件夹。我之前没有将它添加到iOS文件夹中,因此当我在iOS模拟器上运行时它一直在崩溃。

    Image image = new Image();

    public LoginPage()
    {
        Title = "Login"; 
        BackgroundImage = "hands426x640.jpg";}

【讨论】:

  • 您意识到我提供的链接中建议了这一点。正确的?无论如何,我很高兴你知道了。
  • @Nkosi haha​​ 是的,这就是我想尝试的地方 :) 谢谢伙计!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 2019-03-18
  • 2018-07-27
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
相关资源
最近更新 更多