简体   繁体   English

在使用 Xamarin.Essentials 抖动检测事件时,未在 Xamarin.Forms 中从一页导航到另一页

[英]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.

相关问题 将 Xamarin.forms 应用程序上的目标框架从 Android 8.1 更改为 Android 9(对于 Xamarin.Essentials) - Changing target framework on Xamarin.forms app from Android 8.1 to Android 9 (for Xamarin.Essentials) Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个 - Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another 在Xamarin.Forms中将字符串从一页传递到另一页 - Passing a string from one page to another in Xamarin.Forms Xamarin.Forms使用ViewModel导航到另一页 - Xamarin.Forms Navigate to another page using ViewModel 在 Xamarin.Forms 中使用 MVVM 进行页面导航 - Page Navigation using MVVM in Xamarin.Forms Xamarin.Forms 更新条目值来自 Xamarin.Essentials.Contacts 使用 MVVM 选择 - Xamarin.Forms update Entry value from Xamarin.Essentials.Contacts selection using MVVM Xamarin.Forms - Xamarin.Essentials 命名空间缺少 MediaPicker 或 FilePicker 类 - Xamarin.Forms - Xamarin.Essentials namespace missing MediaPicker or FilePicker classes 如何使用Xamarin.Essentials将PDF附加为EmailAttachment? - How to attach a PDF as a EmailAttachment using Xamarin.Essentials? Xamarin.forms离开/更改页面时的事件 - Xamarin.forms event on leaving/changing the page Xamarin.forms导航另一页后如何在同一页上保留图像 - Xamarin.forms how to hold image on same page after navigating another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM