java导入html文件到文本框中,如何将html源同步到文本框?

我已经扫描了标签[cefsharp],我问的问题是以类似的形式出现的(请重新考虑重复的问题行动) .

我无法解决我认为应该是一个简单的请求 . 在浏览器控件中获取页面的html源并同步与winform控件交互 .

我在异步方面很弱,但设法通过我从这个标签[cefsharp]获得的几种方式来获取源代码 . 但是,任何与winform控件的交互都是从异步事件触发的,其中任何后续方法中的异步事件都会调用异步"mode",因此任何winform控件交互 .

Example 1:

private async void Browser_LoadingStateChanged(object sender,

CefSharp.LoadingStateChangedEventArgs e)

{

if (!e.IsLoading)

{

//-- WORKS, but ...

string html = await Task.Run(Browser.GetSourceAsync);

//-- .. when calling other methods which interact with winform controls,

//-- I have to do so in the following way because I am not on the UI thread.

//-- therefore every control needs a block like this

if (InvokeRequired)

{

this.Invoke(new MethodInvoker(delegate

{

txtSource.Text = html;

}));

}

else

{

txtSource.Text = html;

}

}

}

Example 2: 我部分工作的另一个例子是这种方法 . 但是,通过按钮单击调用可以正常工作,而浏览器事件则不会

public string GetSource()

{

string result = null;

if (Browser.IsBrowserInitialized && !Browser.IsDisposed && !Browser.Disposing)

{

Task task = Browser.GetSourceAsync();

var complete = task.ContinueWith(t =>

{

if (!t.IsFaulted)

{

result = t.Result;

}

}, TaskScheduler.Default);

complete.Wait();

}

return result;

}

private void button1_Click(object sender, EventArgs e)

{

//-- WORKS

txtSource.Text = GetSource();

}

private void Browser_LoadingStateChanged(object sender,

CefSharp.LoadingStateChangedEventArgs e)

{

if (!e.IsLoading)

{

//-- DOES NOT WORK. Hangs on waiting for the complete.Wait();

//-- note: this method is not async and neither the button too.

txtSource.Text = GetSource();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值