【发布时间】:2020-09-29 08:27:08
【问题描述】:
我目前正在学习使用 Xamarin.Forms 和 C#(进入它的第一周),并且正在尝试创建一个基本的登录应用程序。
我从一个空白模板开始。现在,当我运行我的应用程序时,只会弹出基本屏幕,而不会弹出页面中的元素。怎么了?
App.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
<?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
using Xamarin.Forms;
namespace XamarinApp
{
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
}
}
}
【问题讨论】:
-
尝试将
new LoginPage();更改为this.MainPage = new LoginPage(); -
@Bijington,这是什么魔法?我的代码有什么问题?另外,我的代码中没有 MainPage.xaml。为什么我还需要使用
this.MainPage? -
您需要定义起始页是什么。
MainPage是App的属性我怀疑您的App.xaml文件中可能已经存在指向页面的内容。 -
@Bijington, this 是我的 App.xaml
-
new LoginPage();只是在内存中创建一个页面的新实例。如果你不将它分配给任何东西,那么它只会留在内存中,直到 GC 清理它。任何 C# 对象都是如此。
标签: c# xamarin.forms execution