Xamarin上的黑屏

HQt*_*izy 2 xaml initializecomponent xamarin.ios xamarin.android xamarin

我有一个Xamarin便携式项目。
我调试的Xaml页面完全空白,在Android和IOS上的页面上都看不到任何组件。

我怎样才能解决这个问题?

注意:没有任何错误消息,页面正在打开,我看不到任何内容。
发生此错误出现了问题。当我修复它时,
尽管调试的页面在InitializeComponent错误之前仍在工作,但它们打开的页面还是空的。

任何帮助将不胜感激。

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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()
        {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Vit*_*nov 5

您应该在ctor中调用InitializeComponent()。否则,UI元素将不会从xaml文件初始化。

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

好的,看来xaml文件中有错误

<Label BackgroundColor="Brown" Text="0,1" Grid.Column="0" Grid.Row="1">
  </Label>
Run Code Online (Sandbox Code Playgroud)

没有棕色,请尝试从这张桌子中选择一些东西

这对我有用

<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>
Run Code Online (Sandbox Code Playgroud)