无法访问函数中的UI控件

 收藏

我从webmethod的c#文件中调用一个函数。但是,当函数尝试访问某些UI控件时,它将引发null引用异常。如何在没有null引用异常的情况下访问UI控件。

        [WebMethod(EnableSession = true)]
        public static string GetIDForDownloadExcel()
        {
            ChartEdit cd = new ChartEdit();    
            Guid clientID = Guid.NewGuid();    
            excelData.Ready = 0;    
            store.Add(clientID, excelData);    
            Thread thread = new Thread(() => cd.ExportToExcelDict(clientID));                
            thread.Start();    
            return JsonConvert.SerializeObject(new { ClientID = clientID, filename = "ExportToExcel.xlsx" });
        }

当程序运行到ExportToExcelDict时,它将尝试访问UI控件,但是,它将引发空引用异常。有什么方法可以访问UI组件?有点想强制首先加载组件。

    private void ExportToExcelDict(Guid clientID)
    {
        bool locationPermission = true;

        List<string> locationIDs = new List<string>(1024);
        List<string> indicatorIDs = new List<string>(1024);

        foreach (RadListBoxItem itm in uiSelectedIndicators.Items) //Null reference happens here
        {
            indicatorIDs.Add(itm.Value);
        }
        foreach (RadListBoxItem itm in uiSelectedLocations.Items)
        {
            locationIDs.Add(itm.Value);
        }
      }   
回复