0
我正在开发使用MVVM Light的WP8.1应用程序。基本上它工作正常。除了导航的一个奇怪的行为。我有一个绑定到一个列表的SelectionChanged事件一个VM的命令:Windows Phone 8.1 MVVM Light Dispose视图
private void GoToTransactionList()
{
if (SelectedAccount != null)
{
((Frame)Window.Current.Content).Navigate(typeof(TransactionList));
}
}
视图不会有任何代码后面的全部内容是用户控制。 这是VM:
public class TransactionListUserControlViewModel : ViewModelBase
{
private Account SelectedAccount
{
get { return ServiceLocator.Current.GetInstance<AccountDataAccess>().SelectedAccount; }
}
public ObservableCollection<FinancialTransaction> RelatedTransactions
{
get { return ServiceLocator.Current.GetInstance<TransactionDataAccess>().RelatedTransactions; }
}
public RelayCommand LoadRelatedTransactionsCommand { get; private set; }
public TransactionListUserControlViewModel()
{
LoadRelatedTransactionsCommand = new RelayCommand(LoadRelatedTransactions);
}
private void LoadRelatedTransactions()
{
ServiceLocator.Current.GetInstance<TransactionDataAccess>().GetRelatedTransactions(SelectedAccount.Id);
}
public void Dispose()
{
this.Cleanup();
}
}
我的定位是这样的:
public TransactionListUserControlViewModel TransactionListControl
{
get { return new TransactionListUserControlViewModel(); }
}
或者:
public TransactionListUserControlViewModel TransactionListControl
{
get { return ServiceLocator.Current.GetInstance<TransactionListUserControlViewModel>(); }
}
我都尝试。但不要改变行为。
现在,我首次导航到列表,我必须单击后退按钮才能导航回去。如果我再次导航到页面,则必须点击两次,依此类推。换句话说,视图不会被丢弃,但是每次我浏览到视图时都会生成一个新的对象。我认为这取决于虚拟机上缺少的东西。
为了更好的概述,我在这里添加链接到github回购。
谁能告诉我我忘了什么吗?
感谢您的帮助!
你不能让后退按钮事件触发的处置? – sexta13 2014-09-02 22:29:58
问题是我不知道我必须处理什么。我假设有一个依赖我没有看到,并防止对象被丢弃或B)我错过了默认命令.. – NPadrutt 2014-09-03 06:59:59
如果你有一个UserControl,我对你的建议是在viewmodellocator中你每次创建一个新的实例。 类似于:public MyUserControlViewModel XPTO {get {return new MyUserControlView();}} – sexta13 2014-09-03 09:19:29