XAML constructor issue? System.Reflection.TargetInvocationException

jddjdd USMember ✭✭✭

Hello,
I meet the error "System.Reflection.TargetInvocationException"
When I execute my code step by step, I observe an unusual behavior:

In the class "HomePageXAML.xaml.cs" (code below), the processor executes the line:

  • "var settingsPage = new SettingsPageXAML();" and runs until the SettingsViewModel class until:
  • function "public string dip_input" until:
  • line "f_dip = _dip;" they goes to:
  • Property "private float _dip" then to line:
  • "get => Preferences.Get("dip", 4.0f);"
    Then the processor goes directly to "await Navigation.PushAsync(settingsPage);" of the HomePageXAML.xaml.cs class instead of going back to the function "public string dip_input"
    with the error "System.Reflection.TargetInvocationException"

I cut the code in multiple lines to find the mistake but I don't understand why it appears.

HomePageXAML.xaml.cs

{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePageXAML : ContentPage
    {
        public HomePageXAML() { ... }
        async void OnButtonTheoryClicked(object sender, EventArgs args) { ... }
        async void OnButtonPracticeClicked(object sender, EventArgs args) { ... }
        async void OnButtonSettingsClicked(object sender, EventArgs args)
        {
            var settingsPage = new SettingsPageXAML(); //goes to SettingsViewModel.cs class OK!!
            await Navigation.PushAsync(settingsPage);    //error "System.Reflection.TargetInvocationException"
        }
    }
}

SettingsViewModel.cs

{
    public class SettingsViewModel : BaseViewModel
    {
        private float _dip
        {
            get => Preferences.Get("dip", 4.0f);   //goes back to "await Navigation.PushAsync(settingsPage);"
                                                                       //of the HomePageXAML.xaml.cs class
                                                                       //instead of going back to the function "public string dip_input"
                                                                       // WHY??
            set { Preferences.Set("dip", value); }
        }
        public string dip_input
        {
            get
            {
                float f_dip;
                string s_dip;
                f_dip = _dip;              //calls Property _dip OK!!
                s_dip = f_dip.ToString("N1", CultureInfo.CurrentCulture); //this line should be executed!!
                return s_dip;
            }
            set { ... }
                this.OnPropertyChanged();
            }
        }

Previously I was saving the Entry Input in the Preferences as a string and it was working. Now I want to convert it to a float and I can't go through.

Best Answer

  • jddjdd US ✭✭✭
    Accepted Answer

    Solved!
    A string was saved in the "dip" key preferences.
    When I tried to get a float, the processor jumped out of the class and came back to HomePageXAML.xaml.cs
    I have uninstalled the App on the VS Emulator to remove all preferences. So now the code works well.

Answers

  • jddjdd USMember ✭✭✭

    This is how the binding is done:
    SettingsPageXAML.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="AlmicantaratXF.Views.SettingsPageXAML"
                 xmlns:i18n="clr-namespace:AlmicantaratXF.Views;assembly=AlmicantaratXF"
                 xmlns:behaviors="clr-namespace:AlmicantaratXF.Behaviors; assembly=AlmicantaratXF"
                 xmlns:viewModels="clr-namespace:AlmicantaratXF.ViewModels; assembly=AlmicantaratXF">
        <ContentPage.Padding><OnPlatform x:TypeArguments="Thickness"><On Platform="iOS" Value="10, 20, 10, 5" /><On Platform="Android, UWP" Value="10, 0, 10, 5" /></OnPlatform></ContentPage.Padding>
        <ContentPage.BindingContext><viewModels:SettingsViewModel/></ContentPage.BindingContext>
        <ContentPage.Content><StackLayout><Grid><Grid.RowDefinitions><RowDefinition Height="*" /><RowDefinition Height="*" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="3*" /><ColumnDefinition Width="*" /><ColumnDefinition Width="2*" /> </Grid.ColumnDefinitions>
                    <Entry Text="{Binding dip_input}" 
                           FontSize="Medium"
                           VerticalOptions="Center"
                           Keyboard="Numeric"
                           MaxLength="4"
                           Grid.Row="0" Grid.Column="1">
                    </Entry>
                    <Entry Text="{Binding index_error_input}" 
                           FontSize="Medium"
                           VerticalOptions="Center"
                           Keyboard="Numeric"
                           MaxLength="4"
                           Grid.Row="1" Grid.Column="1" />
                </Grid>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    BaseViewModel.cs

        public class BaseViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
  • jddjdd USMember ✭✭✭
    Accepted Answer

    Solved!
    A string was saved in the "dip" key preferences.
    When I tried to get a float, the processor jumped out of the class and came back to HomePageXAML.xaml.cs
    I have uninstalled the App on the VS Emulator to remove all preferences. So now the code works well.

  • jezhjezh Member, Xamarin Team Xamurai

    Congrats, and thanks very much for sharing the solution.

Sign In or Register to comment.