Xamarin:模态不弹出
Xamarin: Modal from not popping
几个星期以来,我遇到了一个问题,即提供微调器和标签的模态表单不再弹出。它在旧版本中是 运行,但并未更新所有 NuGet 包。
知道这是什么吗?
有趣的是,它正在处理某些表单而不是其他表单。
protected async override void OnAppearing()
{
try
{
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
//ActivityTracker.StartSpinner(true);
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
你的逻辑是错误的,因为你的 r 进入了一个无限循环,因为 await Navigation.PopModalAsync();回忆一下你弹出的页面的 OnAppearing。
我为您的加载页面推荐另一种解决方案。而不是 push 和 popModel 创建重叠的页面。然后随时显示和隐藏它们。
示例:
<Grid>
<!--here you can add what u want to display on your page-->
<Grid x:Name="LoadingGrid" BackgroundColor="White" IsVisible="false" >
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<ActivityIndicator HeightRequest="50" Color="#2196F3" IsRunning="True" />
</StackLayout>
</Grid>
</Grid>
在你的代码背后
protected async override void OnAppearing()
{
try
{
this.LoadingGrid.IsVisible = true;
//ActivityTracker.StartSpinner(true);
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
this.LoadingGrid.IsVisible = false;
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
很奇怪。我将代码缩减为以下内容:
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
LoopCounter++;
//base.OnAppearing();
//IsFiltered = false;
//LblAttributesVisible = false;
//AllProducts = await App.AllProductsController.GetAllProducts();
//grouped = new ObservableCollection<AssortmentGroups>();
//GroupProducts();
//AllProductsListView.IsGroupingEnabled = true;
//AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
await DisplayAlert("Info", "LoopCounter=" + LoopCounter, "OK");
所以,我们只有 PushModalAsync 和 PopModalAsync。当前所有其他代码不 运行ning。我加了一个loop-counter因为我看到代码是运行ning 2次。如果我 运行 带有 PopModalAsync 的代码,它会 运行 整个代码中的 2 次。如果我删除 PopModalAsync,它仅通过代码 运行s 1 次。也许这就是问题所在。
我也会继续测试
Amjad Saab 解决了这个问题!
我不知道,但在旧版本的 Xamarin Forms 中这似乎有所不同。
问题正如 Amjad 所说:如果我在 onAppearing 中使用 PopModalAsync,它会重新启动 OnAppearing 的覆盖并在循环中结束。
我将代码更改为以下内容:`
public AllProductsListPage()
{
InitializeComponent();
LoadData();
}
protected async void LoadData()
{
try
{
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
所以我所做的是:
- 我将“覆盖 OnAppearing”函数的名称更改为 LoadData()。所以没有OnAppearing功能了
- 运行 InitializeComponent() 之后直接来自页面的 LoadData() 函数
`
几个星期以来,我遇到了一个问题,即提供微调器和标签的模态表单不再弹出。它在旧版本中是 运行,但并未更新所有 NuGet 包。 知道这是什么吗? 有趣的是,它正在处理某些表单而不是其他表单。
protected async override void OnAppearing()
{
try
{
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
//ActivityTracker.StartSpinner(true);
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
你的逻辑是错误的,因为你的 r 进入了一个无限循环,因为 await Navigation.PopModalAsync();回忆一下你弹出的页面的 OnAppearing。
我为您的加载页面推荐另一种解决方案。而不是 push 和 popModel 创建重叠的页面。然后随时显示和隐藏它们。 示例:
<Grid>
<!--here you can add what u want to display on your page-->
<Grid x:Name="LoadingGrid" BackgroundColor="White" IsVisible="false" >
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<ActivityIndicator HeightRequest="50" Color="#2196F3" IsRunning="True" />
</StackLayout>
</Grid>
</Grid>
在你的代码背后
protected async override void OnAppearing()
{
try
{
this.LoadingGrid.IsVisible = true;
//ActivityTracker.StartSpinner(true);
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
this.LoadingGrid.IsVisible = false;
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
很奇怪。我将代码缩减为以下内容:
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
LoopCounter++;
//base.OnAppearing();
//IsFiltered = false;
//LblAttributesVisible = false;
//AllProducts = await App.AllProductsController.GetAllProducts();
//grouped = new ObservableCollection<AssortmentGroups>();
//GroupProducts();
//AllProductsListView.IsGroupingEnabled = true;
//AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
await DisplayAlert("Info", "LoopCounter=" + LoopCounter, "OK");
所以,我们只有 PushModalAsync 和 PopModalAsync。当前所有其他代码不 运行ning。我加了一个loop-counter因为我看到代码是运行ning 2次。如果我 运行 带有 PopModalAsync 的代码,它会 运行 整个代码中的 2 次。如果我删除 PopModalAsync,它仅通过代码 运行s 1 次。也许这就是问题所在。 我也会继续测试
Amjad Saab 解决了这个问题! 我不知道,但在旧版本的 Xamarin Forms 中这似乎有所不同。 问题正如 Amjad 所说:如果我在 onAppearing 中使用 PopModalAsync,它会重新启动 OnAppearing 的覆盖并在循环中结束。 我将代码更改为以下内容:`
public AllProductsListPage()
{
InitializeComponent();
LoadData();
}
protected async void LoadData()
{
try
{
await Navigation.PushModalAsync(new BusyPopUpPage("loading products"));
base.OnAppearing();
IsFiltered = false;
LblAttributesVisible = false;
AllProducts = await App.AllProductsController.GetAllProducts();
grouped = new ObservableCollection<AssortmentGroups>();
GroupProducts();
AllProductsListView.IsGroupingEnabled = true;
AllProductsListView.ItemsSource = grouped;
await Navigation.PopModalAsync();
}
catch (Exception ex)
{
await CreateNewBug.CreateANewBug(ex.Message,"Error in Module " + ex.Source,"\nMessage---\n{ 0}" + ex.Message + "\nInnerException---\n{ 0}" + ex.InnerException + "\nStackTrace---\n{ 0}" + ex.StackTrace + "\nTargetSite---\n{ 0}" + ex.TargetSite);
ToastOptions toastOptions = Message.ShowMessage("An error was raised and a new bug created in our system.", "error");
await this.DisplayToastAsync(toastOptions);
}
}
所以我所做的是:
- 我将“覆盖 OnAppearing”函数的名称更改为 LoadData()。所以没有OnAppearing功能了
- 运行 InitializeComponent() 之后直接来自页面的 LoadData() 函数
`