winrt-xaml - 我们什么时候应该实现 INotifyPropertyChanged
问题描述
我的主页包含视图模型属性,并且主页 xaml 元素通过 x:bind 与视图模型属性绑定。我需要为主页实现 INotifyPropertyChanged 吗?
如果 INotifyPropertyChanged 未在主页中实现,则不存在构建问题。但更新绑定属性有时会引发运行时异常。
但是当我为主页实现 INotifyPropertyChanged 时,一切正常。
property VM::TheViewModel^ ViewModel
{
VM::TheViewModel^ get()
{
return VM::AppData::ViewModel;
}
}
AppData 类提供静态视图模型
ref class AppData
{
public:
static property VM::TheViewModel^ ViewModel
{
VM::TheViewModel^ get()
{
static VM::TheViewModel^ vm= ref new vm::TheViewModel();
return vm;
}
}
}
xaml 数据绑定:
ItemsSource="{x:Bind ViewModel.MyProperty}"
我不确定我为什么要实施
INotifyPropertyChanged
用于主页。当我在视图模型类中有属性时?
从后台线程我停止我的计时器。导航到初始页面,然后在某些条件下返回主页。通过启动计时器并运行后台线程切换回主页时,会发生运行时异常。这不是常规情况。有时会发生有时不会。但它发生了。
OnSomeModelChange()
{
...
if (notifyId == SessionPropertyNotifyType::Session)
{
StopTimers();
AppData::ViewModelInfo->ClearErrors();
auto rootFrame = dynamic_cast<Frame^>(Window::Current->Content);
rootFrame->Navigate(TypeName(InitPage::typeid), nullptr, ref new Animation::SuppressNavigationTransitionInfo);
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Low,
ref new Windows::UI::Core::DispatchedHandler([this]() { InitSessionAsync(); }, CallbackContext::Any)
);
}
...
}
异常:抛出异常:读取访问冲突。
this->__this->**** was 0xDDDDDDDD.
解决方案
推荐阅读
- javascript - 使用 Multer 在 Node.js 中保存使用 Postman 发送的 .wav 文件
- java - 安卓设备或模拟器上的 Files.walkTree 和 File.list()
- c++ - makefile 给了我 collect2: error: 1d returned 1 exit status of other things
- r - 如何计算 R 中每一列数据框的 z 分数?
- split - Cmder 中的 Sublime Text 3 - 如何启动 Sublime 并将终端移动到屏幕的左侧或顶部?
- python - 如何从多个 DataFrame 更新 pandas DataFrame 的计数列?
- java - Hibernate - 如何急切地加载惰性字段?
- r - 当两个特定列总和为零时删除数据帧行
- python - 使用 selenium 和 BeautifulSoap 进行网页抓取
- javascript - 在javascript中以以下形式将javascript嵌套的对象数组与父对象合并?