简体   繁体   English

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

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

Im working on a simple Xamarin Form application after . 之后,我正在处理一个简单的Xamarin Form应用程序。 I created a simple login content page and when i try to run the android emulator im getting this error ' Exception has been thrown by the target of an invocation. 我创建了一个简单的登录内容页面,当我尝试运行android仿真器im时收到此错误- 调用的目标抛出了异常。 ' ,I have Mentioned my code below.Both LoginPage.xaml and LoginPage.xaml.cs ',我在下面提到了我的代码.LoginPage.xaml和LoginPage.xaml.cs

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

I have tested your code, the problem is caused by the <Image> in your xaml. 我已经测试过您的代码,问题是由您的xaml中的<Image>引起的。
The number in Margin should be separated by ',' not '.' 保证金中的数字应以','而不是'。
For example: 例如:

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

On the line where the exception is thrown, try to wrap that line in this: 在引发异常的行上,尝试将行换成这样:

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

Its mainly xaml issue. 主要是xaml问题。

Some property name is given for the control,which is not there. 为控件提供了一些属性名称,该名称不存在。

Implement Try Catch in Initialize Component method & find out. 在Initialize Component方法中实现Try Catch并找出答案。

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

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