本文介绍了Xamarin 上的空白屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

注意:它没有收到错误消息,页面正在打开,但我看不到任何内容.
问题发生在此错误之后.当我修复它时,我调试的页面打开时为空,
虽然他们在 InitializeComponent 错误之前工作.

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.

任何帮助将不胜感激.

这是我的 xaml :

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>

这是我的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);
        }
    }
}

应用程序.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()
        {
        }
    }
}

推荐答案

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

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);
        }
    }
}

好的,貌似xaml文件有问题

Okay,Looks like there is a mistake in xaml file

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

没有棕色,请尝试从此表中选择一些内容

这对我有用

<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>

这篇关于Xamarin 上的空白屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!