In the main page <Home_Page> \
<ContentPage.BindingContext>
<local:ExcelDlj x:Name="viewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<syncfusion:SfDataGrid x:Name="sfDataGrid"
ColumnWidthMode="FitByCell"
AllowTriStateSorting="True"
SortingMode="Single"
GridLinesVisibility="Both"
ItemsSource="{Binding Clients_Colection}"
SelectionMode="SingleDeselect"
CellTapped="sfDataGrid_CellTapped" />
Creates a table the Excel_Dlj
namespace M4.View_Model
{
public class ExcelDlj
{
public ObservableCollection<Client> Clients_Colection { get; set; }
public string Head_Table { get; set; }
public ExcelDlj()
{
Clients_Colection = new ObservableCollection<Client>();
Add_Clients();
}
public void Add_Clients()
{
using ExcelEngine excelEngine = new();
Syncfusion.XlsIO.IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
string desktopPaht2 = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
string filePath = Path.Combine(desktopPaht2, "m.xlsx");
FileStream inputStream = new(filePath, FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
var row1 = worksheet.Rows[0];
Head_Table = (Convert.ToDouble(row1.Cells[3].DisplayText)).ToString("N1", CultureInfo.CreateSpecificCulture("sv-SE"));
for (int R = 1; R < worksheet.Rows.Length; R++)
{
var row = worksheet.Rows[R];
var Cl = new Client(row.Cells[0].DisplayText, row.Cells[1].DisplayText,
row.Cells[2].DisplayText, row.Cells[3].DisplayText,
row.Cells[4].DisplayText, row.Cells[5].DisplayText,
row.Cells[6].DisplayText, row.Cells[7].DisplayText);
Clients_Colection.Add(Cl);
}
inputStream.Dispose();
}
}
}
class property is taken as the source.
What I tried
Thread thread = new(() => { Add_Clients(); });
thread.Start();
And here's the exception:
System.Runtime.InteropServices.COMException: "The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))"`.
And in the end, why do I need this Excel file is large enough has about 350 thousand cells with formulas and has 10 pages. Each page should be loaded separately in the ObservableCollection Clients_Collection which is located in the main thread.
If I'm doing something wrong, please tell me about it.