【问题标题】:Xamarin forms MVVM Remove the previous VM in my current VMXamarin 形成 MVVM 删除我当前 VM 中的前一个 VM
【发布时间】:2019-07-12 09:09:11
【问题描述】:

我有一个主页:HomeVM。 当应用程序启动时,我将 PushModalAsync 用于 RegistrationVM 在我的 RegistrationVM 中,我有一个 pushModalAsync 按钮 -> LoginVM

当用户连接时,我想回到虚拟机 HomeVM,但不可能,使用 PopModalAsync,我回到 RegistrationVM。

我想关闭所有 popmodalasync。

我试过了

在我的 app.xaml 中:

var mainPage = (Page)ViewFactory.CreatePage(typeof(HomeVM));
MainPage = new NavigationPage(mainPage)
{
    BarBackgroundColor = (Color)Resources["PrimaryColor"],
    BarTextColor = Color.White,
};
Navigation = MainPage.Navigation;


protected override void OnStart()
{
    var registrationpage = (Page)ViewFactory.CreatePage(typeof(RegistrationVM));
    MainPage.Navigation.PushModalAsync(registrationpage);
}

注册虚拟机:

public RegistrationVM()
{            
    Task.Run(async () => await ConnectionAPI());   
}

async Task ConnectionAPI()
{
    try
    {
                applicationContext.Device = mydevice;                                           
                await Navigation.PushModalAsync<LoginVM>(async (vm, p) => await vm.InitializeAsync(this));                           
    }
    catch (Exception e)
    {
        Log.Error(e, "Unhandled exception while loading touch points : {e}", e);
    }
    finally
    {       
    }
}

登录虚拟机: 当用户点击“登录” PreviousVM = RegistrationVM。

async Task Login()
{
    try
    {
        Log.Information("Logging in");                 
        applicationContext.User = employe;
        await Navigation.PopModalAsync();
        await Navigation.RemoveAsync(previousVM);
    }
    catch (Exception e)
    {
        Log.Error(e, "Unhandled exception during log in : {e}", e);
    }
    finally
    {       
    }
}

我回到注册视图

【问题讨论】:

  • 可以分享一下ViewFactory的代码吗?

标签: xamarin xamarin.forms navigation


【解决方案1】:

如果您只想弹出所有模态窗口,您应该能够继续调用 PopModalAsync() 直到 Application.Current.MainPage.Navigation.ModalStack.Count 为 0,或者您收到 ArgumentOutofRangeException。

也就是说,您可能需要考虑在此处使用不同的方法。大多数需要登录页面的应用程序不希望在显示时加载任何其他页面。换句话说,您不希望用户能够退回到您的主页,除非他们已登录,因此您不希望堆栈上还有任何其他内容。

为此,您可以将 Application.Current.MainPage 替换为您的登录和/或注册页面。登录完成后,将其替换为应用的主导航页。

【讨论】:

    猜你喜欢
    • 2022-12-17
    • 1970-01-01
    • 2023-03-11
    • 2014-05-25
    • 2020-11-19
    • 1970-01-01
    • 2011-03-25
    • 2013-03-27
    • 2019-03-17
    相关资源
    最近更新 更多