Reputation: 325
Im working on a simple Xamarin Form application after . 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.' ,I have Mentioned my code below.Both LoginPage.xaml and 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");
}
}
}
}
Upvotes: 0
Views: 2971
Reputation: 2168
I have tested your code, the problem is caused by the <Image>
in your xaml.
The number in Margin should be separated by ',' not '.'
For example:
<Image x:Name="LoginIcon" Source="icon.png" Margin="0,80,0,0"/>
Upvotes: 1
Reputation: 120
Its mainly xaml issue.
Some property name is given for the control,which is not there.
Implement Try Catch in Initialize Component method & find out.
Upvotes: 0
Reputation: 33993
On the line where the exception is thrown, try to wrap that line in this:
Device.BeginInvokeOnMainThread (() => {
// Your line(s) with the exception here
}
Upvotes: 0