小编Unc*_*ave的帖子

在另一个线程内操作UI元素

我试图在WinForms C#应用程序中创建一个单独的线程,启动一个控制ProgressBar(marquee)的后台工作程序.问题在于,当我尝试将条形图设置为可见时,它什么都不做,我尝试过多种形式的Invoke,但它们似乎没有帮助.

progressBarCycle从单独的线程调用以下方法.

    BackgroundWorker backgroundWorker = new BackgroundWorker();

    public void progressBarCycle(int duration)
    {
        backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
        backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        backgroundWorker.WorkerReportsProgress = true;
        backgroundWorker.WorkerSupportsCancellation = true;
        backgroundWorker.RunWorkerAsync(duration);
    }

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        worker.ReportProgress(0);

        DateTime end = DateTime.Now.AddMilliseconds((int)e.Argument);
        while (DateTime.Now <= end)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!this.IsHandleCreated)
            this.CreateHandle();
        statusStrip1.Invoke((MethodInvoker)delegate
        {
            progressBar1.Visible = false;
        });
        //    if (!this.IsHandleCreated)
        // …
Run Code Online (Sandbox Code Playgroud)

c# user-interface multithreading invoke winforms

4
推荐指数
1
解决办法
1118
查看次数

在 .net core Web api 和 angular 6 中启用 CORS

在这里,我使用了一个 HTTP POST 请求Angular 6,它在 .net 核心 Web API 调用中命中,但我没有将响应输出返回到Angular 6控制台,并且在控制台中出现以下错误:

“从源‘ http://localhost:4200 ’访问 XMLHttpRequest 在‘ http://localhost:55654/api/login/auth ’已被 CORS 策略阻止:没有‘Access-Control-Allow-Origin’标头是出现在请求的资源上。”

但我有点困惑,如果它是任何CORS启用访问问题,它不会命中 Web API 调用。但它击中了 API 调用,我没有得到响应。

Angular 6 HTTP POST 要求:

this.http.post("http://localhost:55654/api/login/auth", credentials, {
  headers: new HttpHeaders({
 "Content-Type": "application/json"
      })
  }).subscribe(response => {
      let token = (<any>response);
      localStorage.setItem("jwt", token);
    }, err => {

    });
Run Code Online (Sandbox Code Playgroud)

CORS在以下方法中启用了.NetCore Web API

Configure方法Startup.cs

...
app.UseCors(options =>
  options.WithOrigins("http://localhost:4200")
    .AllowAnyMethod()
    .AllowAnyHeader());
 ...
Run Code Online (Sandbox Code Playgroud)

ConfigureServices方法Startup.cs …

c# typescript asp.net-core angular

4
推荐指数
1
解决办法
4167
查看次数

在ASP .NET Core中初始化主机之前,如何读取配置设置?

在初始化应用程序主机之前,我需要从应用程序的配置中读取一些设置以进行其他设置。

在ASP.NET Core 2.x中,在初始化应用程序主机之前读取设置,我曾用来执行以下操作:

public static void Main(string[] args)
{
    //...

    var configuration = new ConfigurationBuilder()
        .AddEnvironmentVariables()
        .AddCommandLine(args)
        .AddJsonFile("appsettings.json")
        .Build();

    //Do something useful with the configuration...

    var host = WebHost.CreateDefaultBuilder()
        .UseStartup<Startup>()
        .UseConfiguration(configuration)
        .Build();

    //...
}

Run Code Online (Sandbox Code Playgroud)

在ASP中,WebHost 不建议使用 .NET Core 3.x ,而应使用.NET Generic Host
.NET通用主机只有一个.ConfigureHostConfiguration().ConfigureAppConfiguration()并且不将内置配置作为参数,而是仅接受用于设置配置的委托。

对于HTTP工作负载,您仍然可以使用该方法.UseConfiguration()公开的方法,IWebHostBuilder并且基本上与以前相同:

public static void Main(string[] args)
{
    //...

    var configuration = new ConfigurationBuilder()
        .AddEnvironmentVariables()
        .AddCommandLine(args)
        .AddJsonFile("appsettings.json")
        .Build();

    //Do something useful with the configuration...

    var host = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-3.0

3
推荐指数
2
解决办法
253
查看次数

Angular 2+无法读取未定义的属性"id"

我有一个奇怪的错误,我无法找出原因.根据:来自路由的id,我正在获取表privateLessons,然后搜索具有正确id的一个对象并显示到我的模板中.最后,我得到了我想要的效果,数据显示正确,但我在控制台中得到两个相同的错误:

ERROR TypeError: Cannot read property 'id' of undefined
at Object.eval [as updateRenderer] (DetailedAnnouncementComponent.html:3)
at Object.debugUpdateRenderer [as updateRenderer] (core.es5.js:13094)
at checkAndUpdateView (core.es5.js:12241)
at callViewAction (core.es5.js:12601)
at execComponentViewsAction (core.es5.js:12533)
at checkAndUpdateView (core.es5.js:12242)
at callViewAction (core.es5.js:12601)
at execEmbeddedViewsAction (core.es5.js:12559)
at checkAndUpdateView (core.es5.js:12237)
at callViewAction (core.es5.js:12601)
Run Code Online (Sandbox Code Playgroud)

如何解决?

详细-announcement.html

  <div class="detailed-announcement">
    {{ privateLesson.id }}
    {{ privateLesson.subject }}
    {{ privateLesson.category }}
    {{ privateLesson.price }}
    {{ privateLesson.level }}
    {{ privateLesson.voivodeship }}
    {{ privateLesson.city }}
    {{ privateLesson.place }}
    {{ privateLesson.description }}
    {{ privateLesson.author }}
  </div>
Run Code Online (Sandbox Code Playgroud)

详细-announcement.tmp

import …
Run Code Online (Sandbox Code Playgroud)

angular

2
推荐指数
1
解决办法
2015
查看次数

React,我如何在 jsx 表达式中嵌入 &lt;br/&gt;

我根据给定的变量呈现不同的文本,我只想
在 h2 标签内的特定变量处呈现。

我尝试使用正则表达式 \r 和 \n (以及组合)进行渲染,但没有用。


render() {

  let {tracker} = this.props, headline;

  switch(tracker){
    case 1:
     headline = 'That is amazing!'
     break;
    case 2:
     headline = 'Too bad.<br />Try again'
     break;
  }

  return(
    <h2>{headline}</h2>
  )
}

Run Code Online (Sandbox Code Playgroud)

javascript reactjs

1
推荐指数
1
解决办法
3927
查看次数