4

我编写了一个应用程序,使用SpeechSynthesizer它可以读出TextBox. 在我的 ViewModel 中,我创建了该类的单个实例并重写Finalize了处理方法。当我在其中粘贴一些文本TextBox并继续单击按钮时,我看到内存使用量不断增加。在我的应用程序中,我有四个按钮Play, Pause, ResumeStop/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>

我的代码有什么问题吗?

4

0 回答 0