UWP XAML 将语音合成声音设置为男性或女性的切换开关

UWP XAML ToggleSwitch to set Speech Synthesis voice to either male or female

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

我声明TG:

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

应该没问题。

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

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
...

我遇到的问题是我可以通过手动设置布尔值 _voiceChoice select 语音,但我无法通过 ToggleSwitch 进行设置。

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

其实我完全看错了。
这是在 Template10 中制作一个可工作的 ToggleSwitch 以在 male/female 语音之间切换所需的内容。可能是更清洁的解决方案,但这有效。

在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);
        }
    }

在 class SettingsPartViewModel : ViewModelBase 中,添加:

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

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

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);
    }