我的应用程序中有几个文本字段,并且当文本的长度小于1时,我希望显示一条错误消息,因此基本上是在文本字段中没有文本的情况下。但是当我进行测试时,应用程序崩溃了。
这是Xaml和C#中的代码(后面的代码)
XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="RoseySports.Create_Activity" Title="Create Activity">
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
Source="background.jpg" Aspect="AspectFill"/>
<ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
<ContentView Padding="0, 40, 0, 0">
<Label Text="Create Activity" FontSize="25" TextColor="White" VerticalOptions="StartAndExpand" HorizontalOptions="CenterAndExpand"/>
</ContentView>
<StackLayout VerticalOptions="Center" HorizontalOptions="Center" Padding="10, 0, 10, 0">
<Grid RowSpacing="50" ColumnSpacing="20" HorizontalOptions="Center" VerticalOptions="Center" Padding="10, 0, 10, 0">
<Label Grid.Row="0" Grid.Column="0" Text="Activity Title" TextColor="White" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"/>
<Entry Grid.Row="0" Grid.Column="1" WidthRequest="200" HorizontalOptions="Center" VerticalOptions="Center" x:Name="title"/>
<Label Grid.Row="1" Grid.Column="0" Text="Date" TextColor="White" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"/>
<DatePicker Grid.Row="1" Grid.Column="1" Format="d MMM yyyy" x:Name="minDate"/>
<Label Grid.Row="2" Grid.Column="0" Text="Place" TextColor="White" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"/>
<Picker x:Name="fieldPicker" Grid.Row="2" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center"/>
<Label Grid.Row="3" Grid.Column="0" Text="Time" TextColor="White" FontSize="20" HorizontalOptions="Center" VerticalOptions="Center"/>
<TimePicker Grid.Row="3" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center"/>
</Grid>
<Label Text="Activity Description" TextColor="White" FontSize="20" Margin="20" HorizontalOptions="Center"/>
<Editor HeightRequest="100" WidthRequest="300" x:Name="description"/>
<Grid VerticalOptions="End" Padding="10, 10, 10, 10">
<Button Grid.Row="0" Grid.Column="0" Text="Return To Home" Clicked="Handle_Clicked" FontSize="20" TextColor="White" HorizontalOptions="Start"/>
<Button Grid.Row="0" Grid.Column="1" Text="Invite" Clicked="Handle_Clicked_1" FontSize="20" HorizontalOptions="End" TextColor="White"/>
</Grid>
</StackLayout>
</ScrollView>
</AbsoluteLayout>
</ContentPage>
C#:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace RoseySports
{
public partial class Create_Activity : ContentPage
{
public Create_Activity()
{
InitializeComponent();
String[] place = {
"Agostinelli Football Field",
"Tennis Court 1 (Rosey)",
"Tennis Court 2 (Rosey)",
"Tennis Court 3 (Rosey)",
"Tennis Court 4 (Rosey)",
"Tennis Court 5 (Rosey)",
"Tennis Court 6 (Rosey)",
"Gymnasium (Rosey)",
"Gymasium (La Combe)",
"Fitness Room (Rosey)",
"Fitness Room (La Combe)",
"Judo Room (La Combe)",
"Swimming Pool (Rosey)",
"Swimming Pool (La Combe)",
"Basketball Court (Rosey)",
"Basketball Court (La Combe)",
"Rugby Field (Rosey)"
};
minDate.SetValue(DatePicker.MinimumDateProperty, DateTime.Now);
for (int i = 0; i < place.Length; i++)
{
fieldPicker.Items.Add(place[i]);
}
}
private async void Handle_Clicked_1(object sender, System.EventArgs e)
{
if (title.Text.Length < 1
&& description.Text.Length < 1)
{
await DisplayAlert("Error", "Make sure all fields are completed", "OK");
} else {
await Navigation.PushModalAsync(new Invite_Page());
}
}
private async void Handle_Clicked(object sender, System.EventArgs e)
{
await Navigation.PushModalAsync(new Home());
}
}
}
最重要的是:
private async void Handle_Clicked_1(object sender, System.EventArgs e)
{
if (title.Text.Length < 1
&& description.Text.Length < 1)
{
await DisplayAlert("Error", "Make sure all fields are completed", "OK");
} else {
await Navigation.PushModalAsync(new Invite_Page());
}
当我尝试将文本框保留为空时,出现此错误:
System.NullReferenceException已引发
对象引用未设置为对象的实例
这是堆栈跟踪:
/ Users / als-moh / Desktop / School / Le Rosey / Class 1&T / SL / CS / IA / RoseySports / RoseySports / Create_Activity.xaml中的RoseySports.Create_Activity + d__1.MoveNext()[0x0001d]中的“ .cs:48 \ n ---从上一个引发异常的位置开始的堆栈结束跟踪--- \ n在/ Library中的System.Runtime.CompilerServices.AsyncMethodBuilderCore + <> c.b__6_0(System.Object状态)[0x00000] /Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 \ n在Foundation.NSAsyncSynchronizationContextDispatcher.Apply() /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/Foundation/NSAction.cs:178 \ n ---从先前引发异常的位置开始的堆栈跟踪结束--- \ n(包装器托管到本地)UIKit.UIApplication.UIApplicationMain(int,string [],intptr,intptr)\ n UIKit.UIApplication.Main(System.String [] args,System.IntPtr主体,System.IntPtr委托) UIKit.UIApplication.Main(System.String [] args上的/Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:79 \ n中的[0x00005] /Library/Frameworks/Xamarin.iOS.framework/Versions/12.0.0.15/src/Xamarin.iOS/UIKit/UIApplication.cs:63 \ n在RoseySports.iOS上的System.String主体类名,System.String委托类名[0x0002c] / Users / als-moh / Desktop / School / Le Rosey / Class 1和T / SL / CS / IA / RoseySports / iOS / Main.cs:17中的.Application.Main(System.String [] args)[0x00001] “
答案 0 :(得分:1)
您应该对title.Text和description.Text进行空检查。因为如果输入控件的文本为空,则该值为null。因此,当您尝试访问length属性时,该应用会崩溃,因为它为null。
if(string.IsNullOrEmpty(title.Text) || string.IsNullOrEmpty(description.Text))
{
await DisplayAlert("Error", "Make sure all fields are completed", "OK");
}
我在if循环中编码了OR(||)比较,因为,如果任何字段为空,则当您需要完成所有字段时,将引发错误警报
答案 1 :(得分:0)
if (title.Text == null && description.Text == null)
{
await DisplayAlert("Error", "Make sure all fields are completed", "OK");
}
因为Entry and Editor文本属性是一个String,所以当它们为空时,它们为null,而length无法检查它是否为null。