【发布时间】:2014-09-02 08:14:32
【问题描述】:
我正在开发一个使用 MVVM Light 的 WP8.1 应用程序。基本上它工作正常。除了导航的奇怪行为。我在绑定到列表的 selectionChanged 事件的 VM 中有一个命令:
private void GoToTransactionList()
{
if (SelectedAccount != null)
{
((Frame)Window.Current.Content).Navigate(typeof(TransactionList));
}
}
视图没有任何代码,整个内容都在用户控件上。 这是虚拟机:
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 存储库的链接。
谁能告诉我我忘记了什么?
感谢您的帮助!
【问题讨论】:
-
不能让后退按钮事件触发dispose吗?
-
问题是我不确切知道我必须处理什么。我假设存在 A)我看不到的依赖项并阻止对象被处理或 B)我想念默认命令..
-
如果你有一个 UserControl,我给你的建议是在 viewmodellocator 中你每次都创建一个新实例。类似于: public MyUserControlViewModel XPTO {get{return new MyUserControlView();}}
-
我添加了一些定位器代码。不是你的意思吗?
标签: mvvm-light windows-phone-8.1