我使用SpeechSynthesizer
写了一个应用程序来读取TextBox
的文本。在我的ViewModel中,我创建了该类的Single实例并覆盖了Finalize
方法以进行处理。当我在TextBox
中粘贴一些文本并继续点击按钮时,我看到内存使用量不断增加。在我的应用程序中,我有四个按钮Play, Pause, Resume
和Stop/Clear
。在我的ViewModel中,我有以下代码
let ss = new SpeechSynthesizer()
let state = self.Factory.Backing( <@ self.State @>, ss.State)
let inputText = self.Factory.Backing(<@ self.InputText @>, "")
let canPlay() = self.State <> SynthesizerState.Speaking &&
self.State <> SynthesizerState.Paused &&
not(String.IsNullOrWhiteSpace(self.InputText))
let canPause() = self.State = SynthesizerState.Speaking
let canResume() = self.State = SynthesizerState.Paused
let canStop() = not(String.IsNullOrWhiteSpace(self.InputText))
let play() = ss.SpeakAsync(self.InputText) |> ignore
self.State <- SynthesizerState.Speaking
let pause() = ss.Pause(); self.State <- SynthesizerState.Paused
let resume() = ss.Resume(); self.State <- SynthesizerState.Speaking
let stop() = if ss.State = SynthesizerState.Paused then ss.Resume()
ss.SpeakAsyncCancelAll()
self.State <- SynthesizerState.Ready
self.InputText <- ""
do
self.DependencyTracker.AddPropertyDependencies( <@ self.Play @>, [ <@@ self.InputText @@> ; <@@ self.State @@> ] )
self.DependencyTracker.AddPropertyDependency( <@ self.Pause @>, <@ self.State @> )
self.DependencyTracker.AddPropertyDependency( <@ self.Resume @>, <@ self.State @> )
self.DependencyTracker.AddPropertyDependency( <@ self.Stop @>, <@ self.InputText @> )
member self.InputText with get() = inputText.Value and set(v) = inputText.Value <- v
member self.State with get() = state.Value and set(v) = state.Value <- v
member self.Play = self.Factory.CommandSyncChecked(play, canPlay)
member self.Pause = self.Factory.CommandSyncChecked(pause, canPause)
member self.Resume = self.Factory.CommandSyncChecked(resume, canResume)
member self.Stop = self.Factory.CommandSyncChecked(stop, canStop)
override self.Finalize() = ss.Dispose()
和Xaml
<TextBox AcceptsReturn="True" TextWrapping="Wrap"
Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalScrollBarVisibility="Auto"/>
<StackPanel Grid.Column="1" Margin="5">
<Button Content="Play" Command="{Binding Play}" />
<Button Content="Resume" Command="{Binding Resume}" Margin="0 5 0 0"/>
<Button Content="Pause" Command="{Binding Pause}" Margin="0 5 0 0"/>
<Button Content="Stop/Clear" Command="{Binding Stop}" Margin="0 5 0 0"/>
</StackPanel>
我的代码有什么问题吗?