【问题标题】:Exception has been thrown by the target of an invocation When Running运行时调用的目标已引发异常
【发布时间】:2018-03-20 09:53:46
【问题描述】:

我正在开发一个简单的 Xamarin Form 应用程序。我创建了一个简单的登录内容页面,当我尝试运行 android 模拟器时,我收到此错误“异常已由调用目标引发。”,我在下面提到了我的代码。 .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");
            }
        }
    }
}

【问题讨论】:

    标签: c# xamarin xamarin.forms xamarin.android


    【解决方案1】:

    我已经测试过你的代码,问题是由你的 xaml 中的&lt;Image&gt; 引起的。
    Margin 中的数字应该用“,”而不是“。”分隔。
    例如:

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

    【讨论】:

      【解决方案2】:

      在抛出异常的那一行,试着把那一行换成这样:

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

      【讨论】:

        【解决方案3】:

        主要是xaml问题。

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

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

        【讨论】:

          猜你喜欢
          • 2019-01-22
          • 2018-11-08
          • 2018-02-14
          • 1970-01-01
          • 2016-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多