C# 如何在MVVM Light xamrin中清除ViewModel的数据?
我现在正在做Xamrin表格。我对ViewModel的清晰数据有问题 当我退出并使用不同的用户登录时,它会显示以前用户的数据,因为C# 如何在MVVM Light xamrin中清除ViewModel的数据?,c#,data-binding,xamarin.forms,mvvm-light,viewmodellocator,C#,Data Binding,Xamarin.forms,Mvvm Light,Viewmodellocator,我现在正在做Xamrin表格。我对ViewModel的清晰数据有问题 当我退出并使用不同的用户登录时,它会显示以前用户的数据,因为UserProfileViewModel的值不清楚 当用户注销时,我想从UserProfileViewModel类文件中清除用户数据。目前,我在用户单击注销时手动执行此操作。我希望任何默认方法(如dispose)都可以清除所有类成员 我尝试用this.Dispose()继承IDisposable接口但这也不起作用 我也尝试过使用默认构造函数,如下所示,但它会抛出错误
UserProfileViewModel
的值不清楚
当用户注销时,我想从UserProfileViewModel类文件中清除用户数据。目前,我在用户单击注销时手动执行此操作。我希望任何默认方法(如dispose)都可以清除所有类成员
我尝试用this.Dispose()继承IDisposable接口代码>但这也不起作用
我也尝试过使用默认构造函数,如下所示,但它会抛出错误
`System.TypeInitializationException`
在app.xaml.cs中的这一行:publicstaticviewmodellocator=>\u Locator??(_locator=新的ViewModelLocator())代码>
在给定的代码中,您可以看到在注销调用时,我调用了方法
`ClearProfileData` of `UserProfileViewModel`
which set default(clear)
数据。它是手动的。我想在用户注销时清除数据
查看模型注销页面
[ImplementPropertyChanged]
公共类LogoutViewModel:ViewModelBase
{
public LogoutViewModel(INavigationService N服务、CurrentUserContext uContext、INotificationService In服务)
{
//初始化所有类成员
私有无效注销()
{
//UserProfileViewModel的调用方法
App.Locator.UserProfile.ClearProfileData();
//注销代码
}
}
}
用户纵断面图模型
[实现属性更改]
公共类UserProfileViewModel:ViewModelBase
{
公共用户档案视图模型(INavigationService N服务、CurrentUserContext uContext、INotificationService In服务)
{
//初始化所有类成员
}
//有没有其他方法可以手动清除数据?
public void ClearProfileData()
{
FirstName=LastName=UserName=string.Empty;
}
}
视图模型定位器
公共类ViewModelLocator
{
静态ViewModelLocator()
{
MySol.Default.Register();
}
public UserProfileViewModel UserProfile=>ServiceLocator.Current.GetInstance();
}
首先,不需要清理这些类型的原始数据类型,gc将为您完成这项工作
但是,如果您使用消息或任何其他强引用,则必须取消订阅,否则viewmodal将挂在内存中,并且永远不会超出范围
垃圾回收器无法收集用户正在使用的对象
应用程序,而应用程序的代码可以到达该对象。这个
据说应用程序对对象有很强的引用
使用Xamarin,这实际上取决于如何将视图耦合到Viewmodals,以确定可能采取的清理Viewmodals的方法
事实证明,MVVM LightViewModelBase
实现了一个iClinup接口,它为您提供了一个可覆盖的清理方法
若要清理其他资源,请重写此方法,清理并
然后调用base.Cleanup()
现在,您只剩下调用ViewModelBase.Cleanup的位置了
如果在DataContextChanged
事件中获得对DataContext
(即ViewModalBase
)的引用,您可以在视图关闭时调用它
或者,您可以连接一个BaseView
,为您解决这个问题,或者您可以实现自己的NagigationService
,它在Pop
上调用Cleanup
。这确实取决于谁在创建视图和视图模型,以及如何耦合它们谢谢您的详细描述。我已尝试将公共虚拟空间清理()
放入UserProfileViewModel
中,并在注销时单击LogoutViewModel
调用此方法。但这没用。如果我把清理方法放错地方,你能建议我吗?我是Xamrin的新手,迈克尔。这是我的错误。我已经替换了我的实例,而不是Messenger实例
,它正在工作。非常感谢你。
`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>();
}
public virtual void Cleanup()
{
// clean up your subs and stuff here
MessengerInstance.Unregister(this);
}