Я испытываю трудности с этой ошибкой, и в мой срок что-то не так с моим XAML или ViewModel, но я не могу определить проблему (в новой версии в xamarin).
Я продолжаю получать эту ошибку System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
at SignUpView.xaml.g.cs: 26
SignUpView.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"
xmlns:ctrls="clr-namespace:MiniPOS.UserControls;assembly=MiniPOS"
xmlns:effects="clr-namespace:MiniPOS.Effects;assembly=MiniPOS"
xmlns:views="clr-namespace:MiniPOS.Views;assembly=MiniPOS"
xmlns:inputLayout="clr-namespace:Syncfusion.XForms.TextInputLayout;assembly=Syncfusion.Core.XForms"
xmlns:combobox="clr-namespace:Syncfusion.XForms.ComboBox;assembly=Syncfusion.SfComboBox.XForms"
xmlns:border="clr-namespace:Syncfusion.XForms.Border;assembly=Syncfusion.Core.XForms"
xmlns:button="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
xmlns:customViews="clr-namespace:MiniPOS.UserControls;assembly=MiniPOS"
xmlns:viewModelBase="clr-namespace:MiniPOS.ViewModels.Base;assembly=MiniPOS"
xmlns:texts="clr-namespace:MiniPOS.Resources.Texts;assembly=MiniPOS"
viewModelBase:ViewModelLocator.AutoWireViewModel="true"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="Sign Up"
x:Name="signUpView"
x:Class="MiniPOS.Views.SignUpView"
>
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="TitleLabelStyle"
TargetType="Label"
BasedOn="{StaticResource SpectralRegularLabelStyle}">
<Setter Property="TextColor"
Value="{StaticResource BlueColor}" />
<Setter Property="HorizontalOptions"
Value="FillAndExpand" />
<Setter Property="FontSize"
Value="{StaticResource LittleSize}" />
<Setter Property="Margin"
Value="0,10,0,0">
</Setter>
</Style>
<Style x:Key="AddCustomerHeaderStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource NunitoRegular}" />
<Setter Property="FontSize"
Value="{StaticResource MidMediumSize}" />
<!--<Setter Property="TextColor"
Value="Gray" />-->
<Setter Property="HorizontalOptions"
Value="FillAndExpand" />
<Setter Property="VerticalOptions"
Value="End" />
<Setter Property="Margin"
Value="0,0,0,0" />
</Style>
<Style x:Key="AddCustomerLabelStyle"
TargetType="{x:Type Label}">
<Setter Property="FontFamily"
Value="{StaticResource NunitoRegular}" />
<Setter Property="FontSize"
Value="{StaticResource LargeSize}" />
<Setter Property="TextColor"
Value="{StaticResource BlackTextColor}" />
<Setter Property="HorizontalOptions"
Value="FillAndExpand" />
<Setter Property="VerticalOptions"
Value="Center" />
<Setter Property="Margin"
Value="0,10,0,0" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<ToolbarItem
Clicked="ToolbarItem_Clicked"
Command="{Binding User_SaveCommand}"
Text="{texts:Translate Save}"
Order="Primary"
Priority="1">
<ToolbarItem.Icon>
<OnPlatform x:TypeArguments="FileImageSource">
<On Platform="Android" Value="save.png" />
</OnPlatform>
</ToolbarItem.Icon>
</ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<Grid>
<ScrollView>
<StackLayout BackgroundColor="White">
<Grid Margin="10,15,10,15">
<Grid.RowSpacing>
<OnPlatform x:TypeArguments="x:Double" Android="-5" iOS="10"/>
</Grid.RowSpacing>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<inputLayout:SfTextInputLayout Grid.Row="0"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
Hint="Username"
ContainerBackgroundColor="Aqua"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry MaxLength="250" Text="{Binding Username}"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="1"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
ContainerBackgroundColor="Aqua"
Hint="Password"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry IsPassword="True" MaxLength="250" Text="{Binding Password}"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="2"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
Hint="First Name"
ContainerBackgroundColor="Aqua"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry MaxLength="250" Text="{Binding FirstName}"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="3"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
ContainerBackgroundColor="Aqua"
Hint="Last Name"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry MaxLength="250" Text="{Binding LastName}"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="4"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
ContainerBackgroundColor="Aqua"
HasError="{Binding IsMobileInValid}"
Hint="{texts:Translate PhoneHint}"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry Text="{Binding PhoneNumber}" Keyboard="Telephone"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="5"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
ContainerBackgroundColor="Aqua"
HasError="{Binding IsEmailInValid}"
Hint="Email"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry Text="{Binding Email}" Keyboard="Email"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
<inputLayout:SfTextInputLayout Grid.Row="6"
ContainerType="Outlined"
FocusedColor="{StaticResource Primary}"
ContainerBackgroundColor="Aqua"
Hint="{texts:Translate AddressHint}"
HorizontalOptions="Fill"
LeadingViewPosition="Inside">
<Entry Text="{Binding Address}" MaxLength="250"/>
<inputLayout:SfTextInputLayout.LeadingView>
<Label FontSize="18"
Text=""
TextColor="{StaticResource Primary}"
VerticalTextAlignment="Center">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String" iOS="Font Awesome 5 Free" Android="fa-solid-900.ttf#Font Awesome 5 Free Solid" >
</OnPlatform>
</Label.FontFamily>
</Label>
</inputLayout:SfTextInputLayout.LeadingView>
</inputLayout:SfTextInputLayout>
</Grid>
</StackLayout>
</ScrollView>
<customViews:LoadingIndicator
Grid.Row="0"
IsRunning="{Binding IsBusy, Mode=TwoWay}"
HorizontalOptions="Fill"
VerticalOptions="Fill" />
</Grid>
</ContentPage.Content>
</ContentPage>
SignUpView.Xaml.cs
using System;
using System.Collections.Generic;
using MiniPOS.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms.Xaml;
namespace MiniPOS.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SignUpView : ContentPage
{
public SignUpViewModel Context => (SignUpViewModel)this.BindingContext;
public SignUpView()
{
InitializeComponent();
NavigationPage.SetHasBackButton(this, false);
}
protected override void OnAppearing()
{
base.OnAppearing();
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
App.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Pan);
}
private void ToolbarItem_Clicked(object sender, EventArgs e)
{
((SignUpViewModel)this.BindingContext).User_SaveCommand.Execute(null);
Navigation.RemovePage(this);
}
SignUpViewModel.cs
using DataModel;
using MiniPOS.AppSettings;
using MiniPOS.Entity;
using MiniPOS.Extensions;
using MiniPOS.Helpers;
using MiniPOS.Resources.Texts;
using MiniPOS.Services;
using MiniPOS.ViewModels.Base;
using MiniPOS.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using System.IO;
namespace MiniPOS.ViewModels
{
public class SignUpViewModel : ViewModelBase
{
private readonly ISignUpService _signUpService;
private User _user;
private List<User> _users;
private UserDetail _userDetail;
private readonly LogWriter log = new LogWriter();
const string filename = "SignUpViewModel.cs";
public SignUpViewModel(ISignUpService signUpService)
{
_signUpService = signUpService;
_user = new User();
_userDetail = new UserDetail();
_users = new List<User>();
MessagingCenter.Subscribe<string>(this, "OnDataSourceChanged", (args) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
IsBusy = true;
await LoadSignUp();
IsBusy = false;
ItemIndex = -1;
});
});
Task.Run(async () =>
{
await LoadSignUp();
});
}
public List<User> Users
{
get => _users;
set
{
_users = value;
RaisePropertyChanged(() => Users);
}
}
public async Task LoadSignUp()
{
Expression<Func<User, bool>> signUpExpression = x => x.Deleted == 0;
var cashs = await _signUpService.GetAllUserAsync(signUpExpression);
}
private string _username;
public string Username
{
get => _username;
set
{
_username = value;
RaisePropertyChanged(() => Username);
}
}
private string _password;
public string Password
{
get => _password;
set
{
_password = value;
RaisePropertyChanged(() => Password);
}
}
private string _firstName;
public string FirstName
{
get => _firstName;
set
{
_firstName = value;
RaisePropertyChanged(() => FirstName);
}
}
private string _lastName;
public string LastName
{
get => _lastName;
set
{
_lastName = value;
RaisePropertyChanged(() => LastName);
}
}
private long _phoneNumber;
public long PhoneNumber
{
get => _phoneNumber;
set
{
_phoneNumber = value;
RaisePropertyChanged(() => PhoneNumber);
}
}
private string _email;
public string Email
{
get => _email;
set
{
_email = value;
RaisePropertyChanged(() => Email);
}
}
private string _address;
public string Address
{
get => _address;
set
{
_address = value;
RaisePropertyChanged(() => Address);
}
}
private void ResetForm()
{
Username = string.Empty;
Password = string.Empty;
PhoneNumber = 0;
Email = string.Empty;
Address = string.Empty;
FirstName = string.Empty;
LastName = string.Empty;
}
public ICommand User_SaveCommand => new Command(async () => await AddUser_SaveAsync());
public async Task AddUser_SaveAsync()
{
string informedMessage = "";
IsBusy = true;
var user = new User
{
Id = Generator.GenerateKey(),
Username = Username,
Password = Password,
IsSuper = true
};
await _signUpService.InsertUserAsync(user);
var userDetail = new UserDetail
{
Id = Generator.GenerateKey(),
UserId = user.Id,
FirstName = FirstName,
LastName = LastName,
Email = Email,
Address = Address,
PhoneNumber = PhoneNumber,
Role = 0,
IsSync = false
};
await _signUpService.InsertUserDetailAsync(userDetail);
ResetForm();
informedMessage = TextsTranslateManager.Translate("CustomerSaved");
log.Info("User Saved", filename);
App._uValid = false;
IsBusy = false;
DialogService.ShowToast(informedMessage);
}
private void ConvertBackModelToEntity()
{
_user.Username = Username;
_user.Password = Password;
_userDetail.FirstName = FirstName;
_userDetail.LastName = LastName;
_userDetail.PhoneNumber = PhoneNumber;
_userDetail.Address = Address;
_userDetail.Email = Email;
}
public override async Task InitializeAsync(object navigationData)
{
IsBusy = true;
PageTitle = TextsTranslateManager.Translate("AddCustomerTitle");
IsBusy = false;
}
}
}
В других вопросах я замечаю, что обычно в xaml есть проблема, но я не могу найти свою ошибку (извините за плохой английский sh)
Вывод:
**System.Reflection.TargetInvocationException:** 'Exception has been thrown by the target of an invocation.'
02-20 16:41:34.738 E/mono (20824):
02-20 16:41:34.738 E/mono (20824): Unhandled Exception:
02-20 16:41:34.738 E/mono (20824): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> TinyIoC.TinyIoCResolutionException: Unable to resolve type: MiniPOS.ViewModels.SignUpViewModel ---> TinyIoC.TinyIoCResolutionException: Unable to resolve type: MiniPOS.ViewModels.SignUpViewModel ---> TinyIoC.TinyIoCResolutionException: Unable to resolve type: MiniPOS.Services.ISignUpService
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer.ResolveInternal (TinyIoC.TinyIoCContainer+TypeRegistration registration, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x0022c] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:3827
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, System.Reflection.ConstructorInfo constructor, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x000ab] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:4031
02-20 16:41:34.738 E/mono (20824): --- End of inner exception stack trace ---
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, System.Reflection.ConstructorInfo constructor, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x000ee] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:4043
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer+MultiInstanceFactory.GetObject (System.Type requestedType, TinyIoC.TinyIoCContainer container, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x00002] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:2858
02-20 16:41:34.738 E/mono (20824): --- End of inner exception stack trace ---
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer+MultiInstanceFactory.GetObject (System.Type requestedType, TinyIoC.TinyIoCContainer container, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x0001d] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:2862
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer.ResolveInternal (TinyIoC.TinyIoCContainer+TypeRegistration registration, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x0002d] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:3736
02-20 16:41:34.738 E/mono (20824): at TinyIoC.TinyIoCContainer.Resolve (System.Type resolveType) [0x00001] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\TinyIoC\TinyIoC.cs:1827
02-20 16:41:34.739 E/mono (20824): at MiniPOS.ViewModels.Base.ViewModelLocator.OnAutoWireViewModelChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x00070] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\ViewModels\Base\ViewModelLocator.cs:158
02-20 16:41:34.739 E/mono (20824): at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463
02-20 16:41:34.739 E/mono (20824): at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397
02-20 16:41:34.739 E/mono (20824): at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:334
02-20 16:41:34.739 E/mono (20824): at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:311
02-20 16:41:34.739 E/mono (20824): at MiniPOS.Views.SignUpView.InitializeComponent () [0x00023] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\obj\Debug
etstandard2.0\Views\SignUpView.xaml.g.cs:26
02-20 16:41:34.739 E/mono (20824): at MiniPOS.Views.SignUpView..ctor () [0x00008] in C:\Users\Juan Finol\source\repos\terminalpointofsale\Mobile\MiniPOS\MiniPOS\Views\SignUpView.xaml.cs:16
02-20 16:41:34.739 E/mono (20824): at (wrapper managed-to-native) System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo,object,object[],System.Exception&)
02-20 16:41:34.739 E/mono (20824): at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:936
02-20 16:41:34.739 E/mono (20824): --- End of inner exception stack trace ---