【发布时间】: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