从BackGroundWorker DoWork WPF VB.NET更改标签文本

时间:2017-12-19 04:30:11

标签: wpf vb.net dispatcher

我的backgroundWorker Dim con As New SqlConnection("Data source=" & My.Settings.ip & "," & My.Settings.port & ";Network Library=DBMSSOCN;initial catalog=offpodb;User id=" & My.Settings.username & ";Password=" & My.Settings.password & ";") con.Open() If connected.Dispatcher.CheckAccess Then connected.Content = "text" Else ' i dunno what to use here as it is not the same as WinForms End If 事件有很多代码要执行。是否可以在执行每个代码后更改标签文本?

例如:

Label1.Invoke((MethodInvoker)delegate {
Label1.Text = i.ToString() + "Files Converted";});

怎么办?

请帮我把这个c#代码转换成WPF VB.NET

<div class="panel-body">
    <div class="col-md-6">
        <p>1. Fill Full Name</p>
        <p>&nbsp;&nbsp;&nbsp; Example</p>
        <label for="name" class="col-sm-2 control-label">Full Name<span class="important">*</span></label>

        <div class="col-sm-6">
            <input type="text" class="form-control" id="name" name="name" size="40" maxlength="50" disabled="disabled"
                value="Full Name" required>
            <div class="help-block with-errors"></div>
        </div>
    </div>
    <div class="col-md-6">
        <p>3. Title #3</p>
        <p>&nbsp;&nbsp;&nbsp; Content for #3</p>
        <label for="name" class="col-sm-2 control-label">Label #3<span class="important">*</span></label>

        <div class="col-sm-6">
            <input type="text" class="form-control" id="name" name="name" size="40" maxlength="50" disabled="disabled"
                value="Label #3" required>
            <div class="help-block with-errors"></div>
        </div>
    </div>
</div>
<!-- Phone Ext -->
<div class="panel-body">
    <p>2. Fill Phone Extention</p>
    <p>&nbsp;&nbsp;&nbsp; Extention</p>
    <label for="name" class="col-sm-1 control-label">Phone Extention<span class="important">*</span></label>

    <div class="col-sm-3">
        <input type="text" class="form-control" id="name" name="name" size="40" maxlength="50" disabled="disabled"
                value="Phone Extention" required>
        <div class="help-block with-errors"></div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您不应使用DoWork方法更新用户界面。

每项任务完成后,在ReportProgress方法中调用DoWork。设置要在UserState参数中显示的消息。

然后处理表单中的ProgressChanged事件,从e.UserState中提取邮件并使用它来更新您的标签。

将其视为后台工作人员通过调用ReportProgress发送邮件,并通过处理ProgressChanged接收邮件的主表单。