Blank screen on Xamarin

非 Y 不嫁゛ 提交于 2019-12-14 01:51:05

问题


I have a Xamarin portable project.
The Xaml pages I debug are totally blank and I cannot see any components on the pages on both Android and IOS.

How can I fix this?

Note: It gets no error messages, the pages are opening and I cannot see anything on them.
The problem occured after this error. When I fixed it the pages I debug become opening empty,
although they were working before InitializeComponent error.

Any help would be greatly appreciated.

This is my 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="AcikAkademi3.Layoutlar.GridOrnek3">

<Grid>
  <Grid.RowDefinitions>  
    <RowDefinition Height="*"></RowDefinition>
    <RowDefinition Height="*"></RowDefinition>
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"></ColumnDefinition>
    <ColumnDefinition Width="*"></ColumnDefinition>
    <ColumnDefinition Width="Auto"></ColumnDefinition>
  </Grid.ColumnDefinitions>

  <Label BackgroundColor="Red" Text="0,0" Grid.Column="0" Grid.Row="0">
  </Label>
  <Label BackgroundColor="Blue" Text="1,0" Grid.Column="1" Grid.Row="0">
  </Label>
  <Label BackgroundColor="Yellow" Text="Açık Akademi" Grid.Column="2" Grid.Row="0"></Label>

  <Label BackgroundColor="White" Text="0,1" Grid.Column="0" Grid.Row="1">
  </Label>
  <Label BackgroundColor="Silver" Text="1,1" Grid.Column="1" Grid.Row="1">
  </Label>
  <Label BackgroundColor="Lime" Text="2,1" Grid.Column="2" Grid.Row="1">
  </Label>

</Grid>
</ContentPage>

This is my cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace AcikAkademi3.Layoutlar
{
    public partial class GridOrnek3 : ContentPage
    {
        public GridOrnek3()
        {
            Padding = new Thickness(0, 20, 0, 0);
        }
    }
}

App.cs :

using AcikAkademi3.Layoutlar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;

namespace AcikAkademi3
{
    public class App : Application
    {
        public App()
        {
            MainPage = new GridOrnek3();
        }

        protected override void OnStart()
        {
        }

        protected override void OnSleep()
        {
        }

        protected override void OnResume()
        {
        }
    }
}

回答1:


You should call InitializeComponent() in ctor. Otherwise UI elements will not init from xaml file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace AcikAkademi3.Layoutlar
{
    public partial class GridOrnek3 : ContentPage
    {
        public GridOrnek3()
        {
            InitializeComponent();
            Padding = new Thickness(0, 20, 0, 0);
        }
    }
}

Okay, Looks like there is a mistake in xaml file

<Label BackgroundColor="Brown" Text="0,1" Grid.Column="0" Grid.Row="1">
  </Label>

There is no Brown color, try to chose something from this table

This works for me

<Grid>
  <Grid.RowDefinitions>  
    <RowDefinition Height="*"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="Auto"/>
  </Grid.ColumnDefinitions>

  <Label    Grid.Column="0" 
            Grid.Row="0"
            BackgroundColor="Red" 
            Text="0,0" />

  <Label    Grid.Column="1" 
            Grid.Row="0"
            BackgroundColor="Blue" 
            Text="1,0" />

  <Label    Grid.Column="2" 
            Grid.Row="0"
            BackgroundColor="Yellow" 
            Text="Açık Akademi"/>

  <Label    Grid.Column="0" 
            Grid.Row="1"
            BackgroundColor="Olive" 
            Text="0,1" />

  <Label    Grid.Column="1" 
            Grid.Row="1"
            BackgroundColor="Silver" 
            Text="1,1" />

  <Label    Grid.Column="2" 
            Grid.Row="1"
            BackgroundColor="Lime" 
            Text="2,1" />

</Grid>



回答2:


In fact, it seems that upon the InitializeComponent line is needed.



来源:https://stackoverflow.com/questions/39344199/blank-screen-on-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!