1

I'm developping a descktop application based en WPF (MVVM pattern) And i'm using MVVMLight-Toolkit, In my application there is a system of user authentication. The problem is: let's say that i have a ViewA, its dataContext is defined by a ViewModelA. Suppose tha User1 is logged to the application and navigates to the viewA, he does some stuff and after that he disconnects. when another user or the same user logon again and navigates to the same ViewA, he finds the lastest context of the previous user. I want to kill the instance of the ViewModel Created and create a new one for the new user. I tried to add a registration method to the ViewModelLocator

public static void RegisterViewModel<T>() where T : ViewModelBase
    {

        if (SimpleIoc.Default.IsRegistered<T>()
            SimpleIoc.Default.Unregister<T>();
        SimpleIoc.Default.Register<T>();
    }

but it doesn't work, the context is stayed. can you help me to solve this problem ? thx

4

1 回答 1

2

我找到了解决方案,我必须释放使用 ViewModel 实例作为数据上下文的视图当我打电话时

SimpleIoc.Default.Unregister();

为类型 T 创建的所有 ViewModel 实例将被自动删除

于 2012-04-16T15:12:26.900 回答