无论我用什么方式编码,都无法在 iPhone 上显示警报,我尝试了 4 种不同的方式
Cannot get Alerts to show on iPhone no matter what way I code it and I have tried 4 different ways
提到的这 4 种方法中,它们都可以在模拟器中使用,但不能在 iPhone 中使用。以下是在这里发挥作用的代码片段。
<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>
查看模型代码
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");
}
公共项目中定义的接口
public interface IMessage
{
void ShowMsg(Mileage value);
void LongAlert(string message);
void ShortAlert(string message);
}
iOS 项目中用于显示警报的代码
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);
}
同样,这在模拟器中有效,但在 iPhone 上它完全没有任何作用。您能给我的任何帮助将不胜感激。非常感谢!
我终于使用推荐的 ItemSelected 让它工作了。这是有效的代码。
景色
<ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" ItemSelected="Selected" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
View背后的代码
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;
}
}
视图模型
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;
}
感谢大家的帮助
提到的这 4 种方法中,它们都可以在模拟器中使用,但不能在 iPhone 中使用。以下是在这里发挥作用的代码片段。
<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>
查看模型代码
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");
}
公共项目中定义的接口
public interface IMessage
{
void ShowMsg(Mileage value);
void LongAlert(string message);
void ShortAlert(string message);
}
iOS 项目中用于显示警报的代码
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);
}
同样,这在模拟器中有效,但在 iPhone 上它完全没有任何作用。您能给我的任何帮助将不胜感激。非常感谢!
我终于使用推荐的 ItemSelected 让它工作了。这是有效的代码。
景色
<ListView x:Name="AutoView" ItemsSource="{Binding AutoData}" ItemSelected="Selected" Grid.Row="1" Grid.ColumnSpan="6" BackgroundColor="Purple">
View背后的代码
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;
}
}
视图模型
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;
}
感谢大家的帮助