每次单击按钮时,以下代码都会触发。它有效,但我意识到每次播放声音时都不应检查声音状态和音量。但是,我似乎无法弄清楚如何让ViewModel以任何其他方式更新ApplicationData。我有一个设置ViewModel,它使用toggleswitch和音量滑块设置声音状态。
理想情况下,我认为,ViewModel会在导航回来时更新类变量,或者设置ViewModel会在此ViewModel中调用更新方法。但我似乎无法让它工作,除了在PlaySound方法中更新它。
update
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
at ServerResponse.header (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:163:12)
at done (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:957:10)
at View.exports.renderFile [as engine] (/home/saunish/WebstormProjects/priceDrop/node_modules/ejs/lib/ejs.js:355:10)
at View.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/view.js:126:8)
at tryRender (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:961:7)
at /home/saunish/WebstormProjects/priceDrop/app.js:130:13
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
at ServerResponse.header (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:163:12)
at done (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:957:10)
at View.exports.renderFile [as engine] (/home/saunish/WebstormProjects/priceDrop/node_modules/ejs/lib/ejs.js:355:10)
at View.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/view.js:126:8)
at tryRender (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/application.js:639:10)
at EventEmitter.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/application.js:591:3)
at ServerResponse.render (/home/saunish/WebstormProjects/priceDrop/node_modules/express/lib/response.js:961:7)
at /home/saunish/WebstormProjects/priceDrop/app.js:168:13
答案 0 :(得分:1)
每当我需要在多个视图模型之间共享行为时(这里,您的行为是“存储和检索设置”),我实现了一个“服务”。在您的情况下,它可能看起来像:
public class SettingsService
{
private readonly ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
private double? localSoundVolume;
public double LocalSoundVolume
{
get
{
if (this.localSoundVolume == null)
{
this.localSoundVolume = Convert.ToDouble(localSettings.Values["localSoundVolumeSetting"])
}
return this.localSoundVolume;
}
set
{
this.localSoundVolume = value;
localSettings.Values["localSoundVolumeSetting"] = value;
}
}
}
然后,您只需将服务注册为单例并在视图模型中检索它。理想情况下,您可以通过使用依赖注入来实现,但如果您使用的任何MVVM框架都没有提供IoC机制,那么服务定位器可以完成这项工作。从那里开始,只需从SettingsViewModel设置SettingsService.LocalSoundVolume
属性并从其他视图模型中读取它即可。由于该值已缓存,因此您只需从ApplicationData设置中检索一次。
遵循此模式,您应该能够从视图模型中删除对ApplicationData的所有引用。