C# 运行时调用的目标引发了异常
我正在做一个简单的Xamarin表单应用程序。我创建了一个简单的登录内容页,当我尝试运行android emulator时,我发现这个错误“调用目标引发了异常。”,我在下面提到了我的代码。LoginPage.xaml和LoginPage.xaml.csC# 运行时调用的目标引发了异常,c#,xamarin,xamarin.forms,xamarin.android,C#,Xamarin,Xamarin.forms,Xamarin.android,我正在做一个简单的Xamarin表单应用程序。我创建了一个简单的登录内容页,当我尝试运行android emulator时,我发现这个错误“调用目标引发了异常。”,我在下面提到了我的代码。LoginPage.xaml和LoginPage.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XLoginApplication.Models;
namespace XLoginApplication.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public LoginPage ()
{
InitializeComponent ();
Init();
}
void Init()
{
BackgroundColor = Constants.BackgroundColor;
Lbl_Username.TextColor = Constants.MainTextColor;
Lbl_Password.TextColor = Constants.MainTextColor;
ActivitySpinner.IsVisible = false;
LoginIcon.HeightRequest = Constants.LoginIconHeight;
Entry_Username.Completed += (s, e) => Entry_Password.Focus();
Entry_Password.Completed += (s, e) => SignInProcedure(s, e);
}
public void SignInProcedure(object sender ,EventArgs e)
{
User user = new User(Entry_Username.Text ,Entry_Password.Text);
if (user.CheckInformation())
{
DisplayAlert("Login", "Login Success", "Oke");
}
else
{
DisplayAlert("Login", "Login Not Suceesfull User name or Password is empty", "Oke");
}
}
}
}
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="XLoginApplication.Views.LoginPage">
<StackLayout x:Name="MasterLayout">
<StackLayout x:Name="LogoStack" VerticalOptions="FillAndExpand">
<Image x:Name="LoginIcon" Source="icon.png" Margin="0.80.0.0"/>
</StackLayout>
<StackLayout x:Name="LoginEntriesStack" VerticalOptions="StartAndExpand">
<StackLayout.Padding>
<OnIdiom x:TypeArguments ="Thickness">
<OnIdiom.Phone>40,0,40,0</OnIdiom.Phone>
<OnIdiom.Tablet>140,150,140,0</OnIdiom.Tablet>
</OnIdiom>
</StackLayout.Padding>
<ActivityIndicator x:Name="ActivitySpinner" Color="Red" IsRunning="True"></ActivityIndicator>
<Label x:Name="Lbl_Username" Text="Username" />
<Entry x:Name="Entry_Username" Placeholder="Username" />
<Label x:Name="Lbl_Password" Text="Password"/>
<Entry x:Name ="Entry_Password" Placeholder="Password" />
<Button x:Name ="Btn_Signin" Text="Sign In" Clicked="SignInProcedure"/>
</StackLayout>
</StackLayout>
</ContentPage>
在引发异常的行上,尝试将该行包装为:
Device.BeginInvokeOnMainThread (() => {
// Your line(s) with the exception here
}
它主要是xaml问题
为控件提供了一些属性名,但不存在
在初始化组件方法中实现Try-Catch并找出答案。我已经测试了您的代码,问题是由xaml中的
引起的。中的数字应以“,”而不是“.”分隔
例如:
<Image x:Name="LoginIcon" Source="icon.png" Margin="0,80,0,0"/>