访问异步任务的datetime值时出现问题



我想在我的谷歌日历上插入一个事件。一切都工作得很好,但唯一的问题是,当我改变我的datetimepicker的值,它不会在执行的时候改变。例如,如果我将datetimepicker的值更改为"2014-07-25",它仍然会在消息框上显示为当前日期。

下面是我的代码:
private void btnsubmitevent_Click(object sender, EventArgs e)
{
    try
    {
        new frm_addeventcal().Run().Wait();
    }
    catch (AggregateException ex)
    {
        foreach (var exi in ex.InnerExceptions)
        {
            MessageBox.Show("ERROR: " + exi.Message);
        }
        MessageBox.Show(ex.InnerException.Message);
    }
}
private async Task Run()
{            
    UserCredential credential;
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
            ClientId = "my cliend id",
            ClientSecret = "my secret key"
        },
        new[] { CalendarService.Scope.Calendar},
        "my gmail id",
        CancellationToken.None
    );
    var service = new CalendarService(new BaseClientService.Initializer() {
        HttpClientInitializer = credential,
        ApplicationName = "project name",
    });
    Event newevent = new Event();
    EventDateTime start = new EventDateTime();
    EventDateTime end = new EventDateTime();
    start.DateTimeRaw = dateTimePicker1.Value.ToString("yyyy-MM-dd") + "T" + dateTimePicker3.Value.ToString("HH:mm:ss");
    MessageBox.Show(start.DateTimeRaw); // here it shows current date although I changed the value of it.
    end.DateTimeRaw = dateTimePicker2.Value.ToString("yyyy-MM-dd") + "T" + dateTimePicker4.Value.ToString("HH:mm:ss");
    MessageBox.Show(end.DateTimeRaw); // same thing happens here too.
    newevent.Summary = "Hello World";
    newevent.Location = "my location";
    newevent.Description = "Random Description";
    newevent.Start = start;
    newevent.End = end;
    var calendarstry = service.Events.Insert(newevent, "calendar id").ExecuteAsync();
}
G

我是新的谷歌api和异步任务。但我认为这个问题是因为我使用异步任务

我认为你的问题是,当你创建表单时,你立即在frm_addeventcal上调用Run方法:

new frm_addeventcal().Run()...

这并没有给你一个机会去改变DateTimePickers的值在它们的值被使用之前,这是令人困惑的,因为你说你改变了值。

你应该这样创建frm_addeventcal:

var form = new frm_addeventcal();
form.ShowDialog();

然后你会想要等到日期时间设置之前调用Run。我认为您想要显示表单,同时让用户更改日期值,只有当用户单击提交时,您才会执行Run:

public class frm_addeventcal : Form
{
    public frm_addeventcal() {
        InitializeComponent();
    }
    private async void btnSubmit_Click(object sender, EventArgs e) {
        await Run();
    }
    private async Task Run() {
        // the DateTimePickers' values should be correct here since this wont 
        // run until submit is clicked
    }
}

相关内容

  • 没有找到相关文章

最新更新