Xamarin Forms 中是否有像 Android 的 Finish() 这样的方法?

我正在寻找一种方法,它可以在我浏览不同页面时结束前一页/活动,以便在按下后退按钮时不会导航回上一条消息。在Java Android中这样的方法叫做finish(),那么在Xamarin Cross Platform中有这样的方法吗?


繁花如伊
浏览 217回答 3
3回答

MMTTMM

Xamarin Forms 有一些选项可以从堆栈中删除或添加页面。看看Navigation.PopAsync或Navigation.RemovePage。

MMMHUHU

根据你如何描述你的目标,我认为这this.Finish();会很有魅力。示例代码:Button nextPage;protected override void OnCreate(Bundle savedInstanceState)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Xamarin.Essentials.Platform.Init(this, savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetContentView(Resource.Layout.your_content_xml);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set our view from the "main" layout resource&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextPage = FindViewById<Button>(Resource.Id.button1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextPage.Click += (s, e) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent nextActivity = new Intent(this, typeof(next_page_CS_file)); // Create Intent instance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartActivity(nextActivity); // Go to the Next Page as set in Intent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.Finish(); // Terminates Current Page.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// As such, once you're in the next page, clicking the back button&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// on phone's menu will close the App instead of going back here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Any code you want that'll handle the Exception.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Like, for example, . . .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }根据我的理解和经验,这应该可行。但是,我不确定是否还有其他方法可以实现您的目标。

ITMISS

在Xamarin Form for Android 上,您有一个类似的方法:Finish&nbsp;();我想你正在寻找这个方法?
打开App,查看更多内容
随时随地看视频慕课网APP