IOS Xamarin forms button works in the simulator but will not work on the iphone

Stephen H 1 Reputation point
2022-03-18T01:20:09.697+00:00

I have a xamarin forms IOS app that runs great in the simulator but when installed on my iphone none of the buttons work. I have app center debugging in there so I can see it never even gets into the button logic. After reading some posts, I even pulled the button out and put it into a separate frame to ensure nothing was overlaying it, but that did nothing. I could really use some help.

Here is my xaml

<ContentPage.BindingContext>
<ViewMode:EnterCarViewModel/>

</ContentPage.BindingContext>
<Grid BackgroundColor="Purple">
<Grid.RowDefinitions>
<RowDefinition Height="47"/>
<RowDefinition Height="30"/>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<RowDefinition Height="35"/>
<RowDefinition Height="45"/>
<RowDefinition Height="30"/>
<RowDefinition Height="45"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
<RowDefinition Height="35"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="105"/>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="65"/>
</Grid.ColumnDefinitions>

<Frame BackgroundColor="#2196F3" Padding="10" CornerRadius="0" Grid.ColumnSpan="6" Grid.Row="0">
    <Label Text="Enter Car" HorizontalTextAlignment="Center" TextColor="White" FontSize="25" />
</Frame>
<Label Text="{Binding EntError}" BackgroundColor="White" HorizontalTextAlignment="Center" TextColor="Red" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6" FontSize="20" />
<Label Text="Year:" TextColor="White" FontSize="Medium" Grid.Row="3" Grid.Column="0" Margin="5" />
<Entry Text="{Binding EntYear}" Keyboard="Numeric" BackgroundColor="White" TextColor="Black"  Grid.Row="3" Grid.Column="1" Margin="0" Grid.ColumnSpan="2" >
    <Entry.Behaviors>
        <Validate:MaxLengthValidator  MaxLength="4"/>
    </Entry.Behaviors>
</Entry>
<Label Text="Name:" TextColor="White" FontSize="Medium" Grid.Row="5" Grid.Column="0" Margin="6"/>
<Entry Text="{Binding EntName}" TextColor="Black" Keyboard="Default" BackgroundColor="White" Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="4" Margin="6" >
    <Entry.Behaviors>
        <Validate:MaxLengthValidator  MaxLength="30"/>
    </Entry.Behaviors>
</Entry>
<Label Text="Default:" TextColor="White" FontSize="Medium" Grid.Row="7" Grid.Column="0" Margin="5" Grid.ColumnSpan="2"/>
<Switch IsToggled="{Binding IsChecked, Mode=TwoWay}" Grid.Column="2" Grid.Row="7" IsVisible="true"/>
<Frame BackgroundColor="#2196F3" Padding="10" CornerRadius="0" Grid.ColumnSpan="3" Grid.Row="9">
    <Button x:Name="MyButton" Command="{Binding AddCarData}" IsEnabled="True"  Text="Add Car" BackgroundColor="Yellow" Grid.Column="1" Grid.ColumnSpan="2" TextColor="Black"/>
</Frame>

</Grid>

Here is the ViewModel which contains the code for the button

    public Command AddCarData => new Command(() =>
    {
        Analytics.TrackEvent("Top of AddCarData. EntYear = " + EntYear + " EntName = " + EntName);

        bool errorFound = false;
        IsVisibleLabel = false;
        IsVisibleResults = false;
        if (EntYear == string.Empty || EntYear == null)
        {
            IsVisibleLabel = true;
            EntError = "You Must Enter Year";
            errorFound = true;
        }
        else if (!errorFound)
        {
            Regex regex = new Regex(@"^[1-9]\d{3,}$");
            Match match = regex.Match(EntYear);
            if (!match.Success && !errorFound)
            {
                IsVisibleLabel = true;
                EntError = "Year Must Be 4 Numbers";
                errorFound = true;
            }
            else if ((Convert.ToInt32(EntYear) < 1900) && !errorFound)
            {
                IsVisibleLabel = true;
                EntError = "You Must Enter a Car Name";
                errorFound = true;
            }
        }
        if (EntName == null && !errorFound)
        {
            IsVisibleLabel = true;
            EntError = "You Must Enter a Car Name";
            errorFound = true;
        }

        Analytics.TrackEvent("In AddCarData. errorFound = " + errorFound);

        if (!errorFound)
        {
            var isDefault = false;
            if (IsChecked)
            {
                isDefault = true;
            }
            else
            {
                isDefault = false;
            }
            MileageItemRepository repository = new MileageItemRepository();
            if (IsChecked)
            {
                var autoResults = repository.GetAuto2();
                foreach (var item in autoResults)
                {
                    if (item.IsDefault)
                    {
                        var resp = repository.UpdateAutoAsync(false, item.Id);
                    }
                }
                App.Current.Properties["processedChange"] = false;
            }
            AutoTableDefination data = new AutoTableDefination()
            {
                IsDefault = isDefault,
                CarYear = EntYear,
                CarDesc = EntName
            };

            int results = repository.AddAutoData(data).Result;

            Analytics.TrackEvent("In AddCarData Results from AddAutoData. results = " + results);

            if (results == 1)
            {
                IsVisibleResults = true;
                EntResults = "Entered Successfully";
                if (Convert.ToBoolean(App.Current.Properties["firstAutoEntered"]))
                {
                    App.Current.Properties["autoId"] = 1;
                    App.Current.Properties["firstAutoEntered"] = false;
                }

                var nav = MyNavigation.GetNavigation();
                nav.PushAsync(new UpdateCars());
            }
            else
            {
                IsVisibleResults = true;
                EntResults = "Error, Please Try Again";
            }
        }
    });

This is really perplexing so if anyone can help me with this, it would be greatly appreciated.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,305 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,417 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Stephen H 1 Reputation point
    2022-03-21T23:08:19.1+00:00

    I solved the situation by changing from a command button click to just a regular click, ie. Clicked="AddCarData". I didn't do this originally as I could not figure out how to get the data in the view model since I was not going straight there, I was now going to the view code behind first. But I found some code that made that possible, here is the code I used.

    void AddCarData(object sender, EventArgs args)
    {
    Button button = (Button)sender;
    var imt = (Grid)button.Parent.Parent;
    var c = (Entry)imt.Children[3];
    var EntYear = c.Text;
    var c2 = (Entry)imt.Children[5];
    var EntName = c2.Text;

    I used this code to get my data from the button, didn't know you could do that, but you can and it works

    0 comments No comments