【问题标题】:How to clear data of ViewModel in MVVM Light xamrin?如何在 MVVM Light xamarin 中清除 ViewModel 的数据?
【发布时间】:2018-07-27 02:56:18
【问题描述】:

我现在正在处理 Xamrin Form。我对 ViewModel 的清晰数据有疑问。

当我注销并以不同的用户登录时,它会显示以前用户的数据,因为 UserProfileViewModel 的值不清楚。

当用户注销时,我想从 UserProfileViewModel 类文件中清除用户数据。目前,当用户单击注销时,我手动执行此操作。我想要像 dispose 这样的任何默认方法来清除所有类成员。

我曾尝试用this.Dispose(); 继承IDisposable 接口,但这也没有用。

我也尝试过如下默认构造函数,但它会抛出错误

`System.TypeInitializationException`

在 app.xaml.cs 的这一行:public static ViewModelLocator Locator => _locator ?? (_locator = new ViewModelLocator());

public UserProfileViewModel()
{
    //initialize all class member
}

在给定的代码中,你可以看到在注销调用时,我调用了方法

`ClearProfileData` of `UserProfileViewModel` 
which set default(clear) 

数据。它是手动的。我想在用户注销时清除数据。

查看模型退出页面

 [ImplementPropertyChanged]
    public class LogoutViewModel : ViewModelBase
    {
        public LogoutViewModel(INavigationService nService, CurrentUserContext uContext, INotificationService inService)
        {
            //initialize all class member
            private void Logout()
            {
                //call method of UserProfileViewModel 
                App.Locator.UserProfile.ClearProfileData();
                //code for logout
            }
        }
    }


User Profile View Model

    [ImplementPropertyChanged]
    public class UserProfileViewModel : ViewModelBase
    {
        public UserProfileViewModel(INavigationService nService, CurrentUserContext uContext, INotificationService inService)
        {
            //initialize all class member
        }

        //Is there any other way to clear the data rather manually?
        public void ClearProfileData()
        {
            FirstName = LastName = UserName = string.Empty;
        }
    }

ViewModel Locator

    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            MySol.Default.Register<UserProfileViewModel>();
        }

        public UserProfileViewModel UserProfile => ServiceLocator.Current.GetInstance<UserProfileViewModel>();
    }

【问题讨论】:

    标签: c# data-binding xamarin.forms mvvm-light viewmodellocator


    【解决方案1】:

    首先不需要清理这些原始数据类型,gc 会为你做这些。

    但是,如果您为此使用消息或任何其他强参考,您必须退订否则,您的视图模态将在内存中徘徊,并且永远不会超出范围

    垃圾收集器无法收集正在使用的对象 应用程序,而应用程序的代码可以到达该对象。这 据说应用程序对该对象具有强引用。

    对于 Xamarin,这实际上取决于您如何将 View 耦合到 Viewmodals 以确定您可能采用哪种方法来清理您的 Viewmodal。

    事实证明 MVVM Light ViewModelBase 实现了一个 ICleanup 接口,该接口具有 可覆盖 清理方法。

    ViewModelBase.Cleanup Method

    要清理其他资源,请覆盖此方法,清理并 然后调用 base.Cleanup()。

    public virtual void Cleanup()
    {
        // clean up your subs and stuff here
        MessengerInstance.Unregister(this);
    }
    

    现在你只剩下调用 ViewModelBase.Cleanup 的地方了

    如果您在 DataContextChanged 事件中获得对 DataContext(即 ViewModalBase)的引用,则可以在视图关闭时调用它

    或者你可以连接一个BaseView 来为你探测这个,或者你可以实现你自己的NagigationService,它在Pop 上调用Cleanup。这确实取决于谁在创建您的视图和视图模型以及您如何耦合它们

    【讨论】:

    • 感谢您提供如此详细的说明。我试图将public virtual void Cleanup() 放入UserProfileViewModel 并在LogoutViewModel 的注销单击时调用此方法。但这没有帮助。如果我把清理方法放在错误的地方,你能建议我吗?我是 Xamrin 的新手。
    • 完成了,迈克尔。这是我的错误。我已经替换了我的实例而不是 MessengerInstance 并且它正在工作。非常感谢。
    猜你喜欢
    • 2013-11-10
    • 2023-03-16
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    相关资源
    最近更新 更多