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