I am currently learning to use Xamarin.Forms and C# (first week into it) and am trying to create a basic login app.
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
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();
}
}
}
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. 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. So that the app when running knows it's starting Page
.
Typically when you create a new Blank Xamarin.Forms app you will see the following code in 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.