[英]Not navigating from one page to another page in Xamarin.Forms while using using Xamarin.Essentials shake detect event
I have two class MainPage.xaml and Home.xaml.我有两个 class MainPage.xaml 和 Home.xaml。 I have written Home.xaml using Xamarin.Essentials shake detect code and want to move from Home to MainPage.xaml but it is not working.
我已经使用 Xamarin 编写了 Home.xaml。Essentials 抖动检测代码并想从 Home 移动到 MainPage.xaml 但它不起作用。
Home.xaml.cs主页.xaml.cs
namespace StarRatingApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Home : ContentPage
{
public Home()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
Accelerometer.ShakeDetected += Accelerometer_ShakeDetected;
Accelerometer.Start(SensorSpeed.Game);
}
private void Accelerometer_ShakeDetected(object sender, EventArgs e)
{
MainThread.BeginInvokeOnMainThread(() =>
{
new NavigationPage(new MainPage());
});
}
}
What am I doing wrong in Accelerometer_ShakeDetected method??我在Accelerometer_ShakeDetected方法中做错了什么?
You are not navigating in the method.您没有在该方法中导航。 You just create a new NavigationPage.
您只需创建一个新的 NavigationPage。 And this won't do anything.
这不会做任何事情。 You have to tell the system, where you want to navigate and how.
你必须告诉系统,你想在哪里导航以及如何导航。
You have two options:你有两个选择:
1) Navigate to your page (this adds the page to the Navigation-Stack) 1)导航到您的页面(这会将页面添加到导航堆栈)
await Navigation.PushAsync(new MainPage());
or 2) Overwrite your mainpage with the new NavigationPage (this removes the whole NavigationStack and starts a "new" navigation)或 2) 用新的 NavigationPage 覆盖您的主页(这会删除整个 NavigationStack 并开始“新”导航)
Application.Current.MainPage = new NavigationPage(new MainPage());
You can find more infos about navigation in the documentation >> https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical您可以在文档中找到有关导航的更多信息 >> https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.