gpt4 book ai didi

javascript - 调试从 JavaScript 调用的 .cs 文件

转载 作者:行者123 更新时间:2023-12-03 02:11:14 32 4
gpt4 key购买 nike

我有一个像这样定义的 Default.aspx,我使用 Chrome 对其进行调试(通过单击“在浏览器中显示 (Google Chrome)”从 VS2017 中启动它。

<%@ Page Language="C#" CodeBehind="Default.aspx.cs" Inherits="DevelopmentWithADot.AspNetSpeechSynthesizer.Test.Default" %>
<%@ Register Assembly="DevelopmentWithADot.AspNetSpeechSynthesizer" Namespace="DevelopmentWithADot.AspNetSpeechSynthesizer" tagPrefix="web" %>
<%@ Register Assembly="System.Speech, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Speech" TagPrefix="web" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>

function onSpeak(text)
{
//document.getElementById('synthesizer').speak(text);
var nSynth = document.getElementById('synthesizer');

if (nSynth == null)
{
debugger; //In internet explorer debugger will be hit directly (if you put a breakpoint). In google chrome, debugger will be hit if the developper tools is open.
alert('Synthesizer is null!');
}

nSynth.speak(text);// I would like to debug what's going on here, but VS doesn't step into my breakpoints in "SpeechSynthesizer.cs"
}

</script>
</head>
<body>
<form runat="server">
<div>
<web:SpeechSynthesizer runat="server" ID="synthesizer" Age="Adult" Gender="Male" Culture="en-US" Rate="0" Volume="100" />
<input type="text" id="text" name="text"/>
<input type="button" value="SpeakIt" onclick="onSpeak(this.form.text.value)"/>
</div>
</form>
</body>
</html>

我现在想调试 SpeechSynthesizer.cs 中发生的情况(因为我没有听到任何输出,我想找出原因)。

所以我想到的第一件事是我需要进入 SpeechSynthesizer.cs,其定义如下:

using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Speech.Synthesis;
using System.Threading;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace DevelopmentWithADot.AspNetSpeechSynthesizer
{
[ConstructorNeedsTag(false)]
public class SpeechSynthesizer : HtmlGenericControl, ICallbackEventHandler
{
private readonly System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();

public SpeechSynthesizer() : base("audio")
{
this.Age = VoiceAge.NotSet;
this.Gender = VoiceGender.NotSet;
this.Culture = CultureInfo.CurrentCulture;
this.VoiceName = String.Empty;
this.Ssml = false;
}

[DefaultValue("")]
public String VoiceName { get; set; }

[DefaultValue(100)]
public Int32 Volume { get; set; }

[DefaultValue(0)]
public Int32 Rate { get; set; }

[TypeConverter(typeof(CultureInfoConverter))]
public CultureInfo Culture { get; set; }

[DefaultValue(VoiceGender.NotSet)]
public VoiceGender Gender { get; set; }

[DefaultValue(VoiceAge.NotSet)]
public VoiceAge Age { get; set; }

[DefaultValue(false)]
public Boolean Ssml { get; set; }

protected override void OnInit(EventArgs e)
{
AsyncOperationManager.SynchronizationContext = new SynchronizationContext();

var sm = ScriptManager.GetCurrent(this.Page);
var reference = this.Page.ClientScript.GetCallbackEventReference(this, "text", String.Format("function(result){{ document.getElementById('{0}').src = result; document.getElementById('{0}').play(); }}", this.ClientID), String.Empty, true);
var script = String.Format("\ndocument.getElementById('{0}').speak = function(text){{ {1} }};\n", this.ClientID, reference);

if (sm != null)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), String.Concat("speak", this.ClientID), String.Format("Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() {{ {0} }});\n", script), true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), String.Concat("speak", this.ClientID), script, true);
}

base.OnInit(e);
}

protected override void OnPreRender(EventArgs e)
{
this.Attributes.Remove("class");
this.Attributes.Remove("src");
this.Attributes.Remove("preload");
this.Attributes.Remove("loop");
this.Attributes.Remove("autoplay");
this.Attributes.Remove("controls");

this.Style[HtmlTextWriterStyle.Display] = "none";
this.Style[HtmlTextWriterStyle.Visibility] = "hidden";

base.OnPreRender(e);
}

public override void Dispose()
{
this.synth.Dispose();

base.Dispose();
}

#region ICallbackEventHandler Members

String ICallbackEventHandler.GetCallbackResult()
{

using (var stream = new MemoryStream())
{
this.synth.Rate = this.Rate;
this.synth.Volume = this.Volume;
this.synth.SetOutputToWaveStream(stream);

if (String.IsNullOrWhiteSpace(this.VoiceName) == false)
{
this.synth.SelectVoice(this.VoiceName);
}
else
{
this.synth.SelectVoiceByHints(this.Gender, this.Age, 0, this.Culture);
}

if (this.Ssml == false)
{
this.synth.Speak(this.Context.Items["data"] as String);
}
else
{
this.synth.SpeakSsml(this.Context.Items["data"] as String);
}

return (String.Concat("data:audio/wav;base64,", Convert.ToBase64String(stream.ToArray())));
}
}

void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
{
this.Context.Items["data"] = eventArgument;
}

#endregion
}
}

但是,Google Chrome 和 VS2017 都不让我进入 SpeechSynthesizer.cs。

我如何调试其中发生的事情?

感谢您的建议。

ps:这是项目层次结构: enter image description here

最佳答案

您无法从 JavaScript 端单步执行它,但在 VS2017 中,您可以在 SpeechSynthesizer(或任何其他您的服务器端类)。当您从使用该构造函数/方法的 Chrome 发出请求时,VS2017 将在断点处停止,让您单步执行逻辑。

关于javascript - 调试从 JavaScript 调用的 .cs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49560573/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com