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



我正在尝试通过 Template10 UWP 应用程序的设置页面中的切换开关将 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
...

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

我也意识到这个解决方案不是很干净,我愿意接受任何建议。任何帮助将不胜感激。提前谢谢。

实际上,我完全错误地看待这个问题。
以下是在 Template10 中制作工作切换开关以在男声/女声之间切换所需的内容。可能是更干净的解决方案,但这有效。

在"设置页.xaml"中,添加:

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

在"设置服务.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(); }
    }

最后,在单独的类中,设置 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);
    }

最新更新