'Cannot get Alerts to show on iPhone no matter what way I code it and I have tried 4 different ways
Of these 4 methods mentioned, they all work in the simulator, but non of them work on the iPhone. Here are the pieces of code that come into play here.
<ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" SelectedItem="{Binding SelectedItem}" SeparatorVisibility="None" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="27"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="105"/>
<ColumnDefinition Width="35"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="36"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Year}" HorizontalTextAlignment="Center"/>
<Label Grid.Column="2" Grid.Row="0" FontSize="Medium" TextColor="White" Text="{Binding Name}" Grid.ColumnSpan="2" HorizontalTextAlignment="Start"/>
<Switch x:Name="{Binding Id}" IsToggled="{Binding IsChecked, Mode=TwoWay}" Toggled="Handle_Toggled" Grid.Column="6" Grid.Row="0" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
View Model code
public AutoWithSwitch SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
if (_selectedItem == null)
return;
DisplayAlerts(value);
}
}
public async void DisplayAlerts(AutoWithSwitch value)
{
Application.Current.Properties["DeleteData"] = value;
DependencyService.Get<IMessage>().LongAlert("Stupid");
}
Interface defined in the common project
public interface IMessage
{
void ShowMsg(Mileage value);
void LongAlert(string message);
void ShortAlert(string message);
}
Code in iOS project to display the alert
public void LongAlert(string message)
{
ShowAlert(message);
}
public void ShortAlert(string message)
{
ShowAlert(message);
}
void ShowAlert(string message)
{
UpdateCarsViewModel ucvm = new UpdateCarsViewModel();
//alertDelay = NSTimer.CreateScheduledTimer(seconds, (obj) =>
//{
// dismissMessage();
//});
alert = UIAlertController.Create(null, "Action", UIAlertControllerStyle.Alert);
alert.AddAction(UIAlertAction.Create("Delete", UIAlertActionStyle.Default,UIAlertAction => ucvm.DeleteCar()));
alert.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alert, true, null);
}
Again, this works in the simulator but on the iPhone it does absolutely nothing. Any help you could give me would be much appreciated. Thanks so much!
Solution 1:[1]
I finally got it working using the ItemSelected that was recommended. Here is the code that worked.
The View
<ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" ItemSelected="Selected" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
The View code behind
void Selected(object sender, SelectedItemChangedEventArgs e)
{
if (Convert.ToBoolean(Application.Current.Properties["FirstSelected"]))
{
Application.Current.Properties["FirstSelected"] = false;
//Analytics.TrackEvent("UpdateCar.xaml.cs - Select top");
var ucvm = new UpdateCarsViewModel((AutoWithSwitch)e.SelectedItem);
//Analytics.TrackEvent("UpdateCar.xaml.cs - Select top2" + (AutoWithSwitch)e.SelectedItem);
if (e.SelectedItem == null)
{
return;
}
AutoView.SelectedItem = null;
}
}
The View Model
public UpdateCarsViewModel(AutoWithSwitch data)
{
StackTrace stackTrace = new StackTrace();
// Get calling method name
var methodName = stackTrace.GetFrame(1).GetMethod().Name;
Analytics.TrackEvent("In UpdateCarsViewModel MethodName = " + methodName);
if (methodName == "Selected")
{
DisplayAlerts(data);
}
}
public async void DisplayAlerts(AutoWithSwitch value)
{
var answer = await (App.Current as App).MainPage.DisplayAlert("Action", "Delete", "Ok", "Cancel");
if (answer)
DeleteCar(value);
else
Application.Current.Properties["FirstSelected"] = true;
}
Thanks to everyone for all your help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Steve H |