xamarin.forms - 弹出另一个页面后,Xamarin.Forms 页面不会立即出现
问题描述
我的应用程序目前有三个页面。我将它们称为EventsPage、NewEventPage 和ListPage。EventsPage 是应用程序的第一页,您可以从那里打开一个 NewEventPage。在这个 NewEventPage 上有一个按钮,它从堆栈中弹出 NewEventPage 并且应该在之后立即创建一个 ListPage,但是 ListPage 没有出现,尽管我发现它的构造函数正在运行。
这是 NewEventPage 的代码:
using Partylist.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Partylist.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NewEventPage : ContentPage
{
// Constructor.
public NewEventPage()
{
InitializeComponent();
}
// Event handlr for when the "Cancel" button is clicked.
async void OnCancelClicked(object sender, EventArgs e)
{
// Goes back to the previous page.
await Navigation.PopAsync();
}
// Event handler for when the "Create" button gets clicked.
async void OnCreateClicked(object sender, EventArgs e)
{
// Make sure there is something in the text entry.
if (string.IsNullOrWhiteSpace(EventNameEntry.Text))
{
// If there is nothing there, print an error message.
ErrorLabel.Text = "Your event needs a name!";
}
// If there is something in the text entry, try to create
// a new event with the text as its name.
else
{
// Variable to store the created event.
Event newEvent = new Event();
// Variable to store its folder.
DirectoryInfo newEventFolder;
// Flag to see if the event was created sccessfully.
bool eventCreated;
try
{
// If there's already an event with that name, let the
// user know.
if
(Directory.Exists(Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
EventNameEntry.Text)))
{
ErrorLabel.Text = "You already have an event with that name.";
eventCreated = false;
}
// Otherwise, try to creaate the folder.
else
{
newEventFolder = Directory.CreateDirectory(
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
EventNameEntry.Text));
// Then, create the event based on that folder.
newEvent = new Event
{
FolderName = EventNameEntry.Text,
DateCreated = newEventFolder.CreationTime,
DateEdited = newEventFolder.LastWriteTime
};
// Get rid of any error messages that might be on screen.
ErrorLabel.Text = "";
eventCreated = true;
}
}
// Should throw an ArgumentException in most cases where there is
// an invalid character.
catch (ArgumentException)
{
// Get all the invalid characters and turn them into a string.
char[] invalidChars = Path.GetInvalidPathChars();
string invalid = "";
foreach(char currentChar in invalidChars)
{
invalid += currentChar;
}
// Change the text of the error label.
ErrorLabel.Text = "Your event name can't have these characters: \""
+ invalid + "\".";
eventCreated = false;
}
// If the event was created successfully, select it, pop the "New Event"
// page, and open a "List" page for the event.
if (eventCreated)
{
App.selectedEvent = newEvent;
await Navigation.PopAsync();
await Navigation.PushAsync(new ListsPage());
}
}
}
}
}
这是 ListPage 的代码:
using Partylist.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Partylist.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ListsPage : ContentPage
{
// List of lists, used to populate
// the page's ListView (see the XAML).
public ObservableCollection<PartylistList> ListList { get; set; }
// Constructor.
public ListsPage()
{
// Does all the stuff to make the page
// exist that doesn't involve anything
// specific to this particular page in
// this particular app.
InitializeComponent();
}
// Override for OnAppearing().
protected override void OnAppearing()
{
// Regular OnAppearing() method.
base.OnAppearing();
// Set the title to be the name of the selected event.
Title = App.selectedEvent.FolderName;
// Set the BindingContext of the page to itself.
this.BindingContext = this;
// Set the ItemsSource of the ListView in the
// XAML to the ObservableCollection.
ListList = new ObservableCollection<PartylistList>();
ListListView.ItemsSource = ListList;
// Loop to populate the ObservableCollection.
for (int i = 0; i < Directory.GetFiles(
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
App.selectedEvent.FolderName))
.Length; i++)
{
// Add a new list.
ListList.Add(new ContactList());
// Set the filename to the name of the file
// that the list corresponds to.
ListList.ElementAt(i).Filename =
Directory.GetFiles(
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
App.selectedEvent.FolderName))[i];
// Sets the date/time created to the file's
// creation date.
ListList.ElementAt(i).DateCreated = Directory
.GetCreationTime(Directory.GetFiles(
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
App.selectedEvent.FolderName))[i]);
// Sets the date/time last edited to the
// folder's write date.
ListList.ElementAt(i).DateEdited = Directory
.GetLastWriteTime(Directory.GetFiles(
Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.LocalApplicationData),
App.selectedEvent.FolderName))[i]);
}
}
}
}
解决方案
推荐阅读
- scala - 如何在scala中避免双舍入和科学记数法?
- java - Spring Cache - @CacheEvict,@CachePut 在从同一类的另一个方法调用时不起作用
- acumos - 为什么 Acumos 上的 ML Workbench 未启用?
- javascript - 在边框内移动文本
- android - 在 WebView 上设置监听器 | 原生脚本 + vue
- java - 原因:java.io.IOException:HTTP/1.1 标头解析器未收到任何字节
- r - 对 R 中的点矩阵进行排序以获得用于在 xy 图上绘制线的顺序向量
- integration-testing - Cypress Test-Runner:隐藏详细消息,而不是展开?
- mongodb - 验证猫鼬中的字符串数组
- docker - Docker容器在启动后立即崩溃,而图像构建时包含源代码的错误