繁体   English   中英

运行时,调用的目标已引发异常

[英]Exception has been thrown by the target of an invocation When Running

之后,我正在处理一个简单的Xamarin Form应用程序。 我创建了一个简单的登录内容页面,当我尝试运行android仿真器im时收到此错误- 调用的目标抛出了异常。 ',我在下面提到了我的代码.LoginPage.xaml和LoginPage.xaml.cs

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>

LoginPage.xaml.cs

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

我已经测试过您的代码,问题是由您的xaml中的<Image>引起的。
保证金中的数字应以','而不是'。
例如:

<Image x:Name="LoginIcon" Source="icon.png" Margin="0,80,0,0"/>

在引发异常的行上,尝试将行换成这样:

Device.BeginInvokeOnMainThread (() => {
    // Your line(s) with the exception here
}

主要是xaml问题。

为控件提供了一些属性名称,该名称不存在。

在Initialize Component方法中实现Try Catch并找出答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM