简体   繁体   English

Xamarin.Forms:应用程序不在页面上显示元素

[英]Xamarin.Forms: App does not show elements on the page

I am currently learning to use Xamarin.Forms and C# (first week into it) and am trying to create a basic login app.我目前正在学习使用 Xamarin.Forms 和 C#(第一周),并正在尝试创建一个基本的登录应用程序。
I started with a blank template.我从一个空白模板开始。 Now, when I run my app, only the basic screen pops up, but not the elements in the page.现在,当我运行我的应用程序时,只弹出基本屏幕,而不是页面中的元素。 What is wrong?怎么了?

App.xaml.cs应用程序.xaml.cs

using Xamarin.Forms;

namespace XamarinApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            new LoginPage();
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}


LoginPage.xaml登录页面.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="XamarinApp.LoginPage">
    <ContentPage.Content>
        <StackLayout>
            <Label 
                Text="Welcome"
                />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

LoginPage.xaml.cs登录页面.xaml.cs

using Xamarin.Forms;

namespace XamarinApp
{
    public partial class LoginPage : ContentPage
    {
        public LoginPage()
        {
            InitializeComponent();
        }

    }
}

TL-DR TL-DR

I believe that to solve your problem you will need to change your line:我相信要解决您的问题,您需要更改您的线路:

new LoginPage();

to:到:

MainPage = new LoginPage();

My reasoning我的推理

You need to specify what the MainPage is.您需要指定MainPage是什么。 This part can be seen as confusing when you first start out because the example page that is supplied is also called MainPage but one is a class/Page implementation and the other key part is a property provided by the Application class that your App class inherits from.这部分在您刚开始时可能会被视为令人困惑,因为提供的示例页面也称为MainPage但一个是class/Page实现,另一个关键部分是您的App类继承自的Application类提供的属性. So that the app when running knows it's starting Page .以便应用程序在运行时知道它正在启动Page

Typically when you create a new Blank Xamarin.Forms app you will see the following code in App.xaml.cs通常,当您创建新的空白 Xamarin.Forms 应用程序时,您将在 App.xaml.cs 中看到以下代码

public App()
{
    InitializeComponent();

    MainPage = new MainPage();
}

I suspect that your changes to add in your LoginPage somehow ended up removing the key part: MainPage = from that line.我怀疑您在 LoginPage 中添加的更改以某种方式最终从该行中删除了关键部分: MainPage =

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM