【问题标题】:UWP XAML ToggleSwitch to set Speech Synthesis voice to either male or femaleUWP XAML ToggleSwitch 将语音合成语音设置为男性或女性
【发布时间】:2016-08-08 07:46:03
【问题描述】:

我正在尝试通过 Template10 UWP 应用的设置页面中的 ToggleSwitch 将 voice.gender 设置为男性或女性。

我声明 TG:

<ToggleSwitch x:Name="VoiceSelection" Header="Select Voice"
                                  IsOn="{Binding VoiceChoice, Mode=TwoWay}"
                                  OffContent="Male Voice" OnContent="Female Voice" />

应该没问题。

然后我设置一个布尔值,稍后将用于选择男性或女性

public event PropertyChangedEventHandler PropertyChanged;
public static bool _voiceChoice = true;
public bool VoiceChoice
    {
        get
        {
            return _voiceChoice;
        }
        set
        {
            _voiceChoice = value;
            OnPropertyChanged("VoiceChoice");
        }
    }

protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

有关信息,这是稍后分配语音的代码。这也很好用。

...
if (_voiceChoice == true)
                {
                    VoiceInformation voiceInfo =
                      (
                        from voice in SpeechSynthesizer.AllVoices
                        where voice.Gender == VoiceGender.Female
                        select voice
                      ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                    synthesizer.Voice = voiceInfo;
                    stream = await synthesizer.SynthesizeTextToStreamAsync(text);
                }
                else
...

我的问题是我可以通过手动设置 boolean _voiceChoice 来选择声音,但我无法通过 ToggleSwitch 进行设置。

我也意识到这个解决方案不是很干净,我愿意接受任何建议。任何帮助是极大的赞赏。提前致谢。

【问题讨论】:

  • “无法通过 ToggleSwitch 设置”是什么意思?有错误吗?属性的setter没有被调用吗?
  • 代码中有问题,希望我知道.. :)
  • 如果在属性设置器中设置断点并切换开关,断点会被命中吗?
  • 你的DataContext设置了吗?没有数据上下文,绑定不起作用

标签: c# binding uwp-xaml speech-synthesis toggleswitch


【解决方案1】:

实际上,我完全看错了。
这是在 Template10 中制作工作切换开关以在男声/女声之间切换所需的内容。可能是更清洁的解决方案,但这是可行的。

在 SettingsPage.xaml 中,添加:

<ToggleSwitch x:Name="VoiceSelection" Header="Select Voice"
    IsOn="{Binding UseVoiceSelection, Mode=TwoWay}"
    OffContent="Male Voice" OnContent="Female Voice" />

在 SettingsService.cs 中,添加:

public bool UseVoiceSelection
    {
        get { return _helper.Read<bool>(nameof(UseVoiceSelection), true); }
        set
        {
            _helper.Write(nameof(UseVoiceSelection), value);
        }
    }

在 SettingsPartViewModel : ViewModelBase 类中,添加:

public bool UseVoiceSelection
    {
        get { return _settings.UseVoiceSelection; }
        set { _settings.UseVoiceSelection = value; base.RaisePropertyChanged(); }
    }

最后,在一个单独的类中,设置布尔值并进行语音合成:

public class ReadSpeech
{
    public static bool _voiceChoice = true;

    // Performs synthesis
    async Task<IRandomAccessStream> SynthesizeTextToSpeechAsync(string text)
    {
        IRandomAccessStream stream = null;
        using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
        {
            if (temp.SettingsPartViewModel.UseVoiceSelection == true)
            {
                VoiceInformation voiceInfo =
                  (
                    from voice in SpeechSynthesizer.AllVoices
                    where voice.Gender == VoiceGender.Female
                    select voice
                  ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                synthesizer.Voice = voiceInfo;
                stream = await synthesizer.SynthesizeTextToStreamAsync(text);
            }
            else
            {
                VoiceInformation voiceInfo =
                  (
                    from voice in SpeechSynthesizer.AllVoices
                    where voice.Gender == VoiceGender.Male
                    select voice
                  ).FirstOrDefault() ?? SpeechSynthesizer.DefaultVoice;
                synthesizer.Voice = voiceInfo;
                stream = await synthesizer.SynthesizeTextToStreamAsync(text);
            }
        }
        return (stream);
    }

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 1970-01-01
    • 2017-09-16
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多