有没有办法回到导航堆栈中的最后一页,而不是在Xamarin中执行veraperation方法?

发布于 2025-02-12 03:52:55 字数 687 浏览 2 评论 0原文

因此,我拥有page1,其中一些输入字段已经填写,并且有一个按钮打开一个新页面(让我们称其为page2)。

这是我用来使用page2的代码。

Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Page2()));

这是我过去回到page1上的代码。

private async void GoBackButton_Clicked(object sender, EventArgs e)
    {
        await Application.Current.MainPage.Navigation.PopModalAsync();
    }

现在,我想以某种方式,当用户完成他在page2上的内容时,他按下按钮,称为gooback,然后他回到上。 page1 page1的viewModel的方法是 not 被执行。这可行吗?

不确定是否重要,但是我在Windows10上使用VS22。

So, I have Page1, with some input fields that my user already filled, and, there is a button that opens a new Page (let's call it Page2).

This is the code I used to go on Page2.

Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Page2()));

This is the code I used to came back on Page1.

private async void GoBackButton_Clicked(object sender, EventArgs e)
    {
        await Application.Current.MainPage.Navigation.PopModalAsync();
    }

Now, I'd like to, somehow, when user finishes what he had on Page2, he presses the button, called GoBack, then he comes back on the Page1, and the OnApearing method of Page1's ViewModel is NOT getting executed. Is this doable?

Not sure if important, but I'm using VS22, on Windows10.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2025-02-19 03:52:55

这就是我在某些应用中使用的。逻辑是:Page1在第一个加载上加载数据,然后在请求重新加载时仅重新加载数据。

这是一个示例:

page1.xaml.cs

public partial class Page1 : ContentPage
{
    private bool _isFirstLoad = true;
    private bool _isReloadRequested;

    public Page1()
    {
        InitializeComponent();
    }

    private async Task OpenPage2Async()
    {
        //navigate to Page2
        await Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Page2(() => _isReloadRequested = true)));
    }

    protected override void OnAppearing()
    {
        if (_isFirstLoad)
        {
            _isFirstLoad = false;
            ReloadData();
            return;
        }

        if (_isReloadRequested)
        {
            ReloadData();
            _isReloadRequested = false;
        }
    }

    private void ReloadData()
    {
        //reload data...
    }
}

page2.xaml.cs

public partial class Page2 : ContentPage
{
    private readonly Action _requestReload;

    public Page2(Action requestReload)
    {
        InitializeComponent();
        _requestReload = requestReload;
    }

    private async Task GoBackAsync()
    {
        //invoke callback to set Page1's _isReloadRequested to true
        _requestReload?.Invoke();
        //go back to Page1
        await Application.Current.MainPage.Navigation.PopModalAsync();
    }
}

This is what I'm using in some of my apps. The logic is: Page1 loads data on the first load and then only reloads data when reload requested.

Here's an example:

Page1.xaml.cs

public partial class Page1 : ContentPage
{
    private bool _isFirstLoad = true;
    private bool _isReloadRequested;

    public Page1()
    {
        InitializeComponent();
    }

    private async Task OpenPage2Async()
    {
        //navigate to Page2
        await Application.Current.MainPage.Navigation.PushModalAsync(new NavigationPage(new Page2(() => _isReloadRequested = true)));
    }

    protected override void OnAppearing()
    {
        if (_isFirstLoad)
        {
            _isFirstLoad = false;
            ReloadData();
            return;
        }

        if (_isReloadRequested)
        {
            ReloadData();
            _isReloadRequested = false;
        }
    }

    private void ReloadData()
    {
        //reload data...
    }
}

Page2.xaml.cs

public partial class Page2 : ContentPage
{
    private readonly Action _requestReload;

    public Page2(Action requestReload)
    {
        InitializeComponent();
        _requestReload = requestReload;
    }

    private async Task GoBackAsync()
    {
        //invoke callback to set Page1's _isReloadRequested to true
        _requestReload?.Invoke();
        //go back to Page1
        await Application.Current.MainPage.Navigation.PopModalAsync();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文