xamarin, How to smoothly upload content to StackLayout

早过忘川 提交于 2020-06-09 05:45:06

问题


I have a large database and I need to organize searching accounts from my application. App is loaded data dynamically. NetworkBackgroundWorker is a class that send requeste to the server (with database). This class uses BackgroundWorker for waiting answer in the background.

//Callback method
Action<string> refToSP = SetParticipants;
//Send a request to the server for getting account data
NetworkBackgroundWorker.InvokeService(
                query,
                requestURL,
                methodName,
                refToSP);

The SetParticipants method is using another BackgroundWorker for add new accounts to the mainStackLayout in the background.

//SetParticipants - This is a method that will be call from the BackgroundWorker
//participantsJSON - Data represented as a JSON code
public void SetParticipants(string participantsJSON)
        {
            backgroundWorker = new BackgroundWorker();

            backgroundWorker.DoWork +=
                new DoWorkEventHandler(backgroundWorker_DoWork);

            backgroundWorker.RunWorkerAsync(participantsJSON);
        }

And the method for adding new accounts to the MainStackLayout

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List<ParticipantsTable> participantsTable =
                JsonSerializer.Deserialize<List<ParticipantsTable>>(e.Argument.ToString());

            for (int i = 0; i < participantsTable.Count; i++)
            {
                BoxView boxView = new BoxView();
                boxView.BackgroundColor = Color.LightGreen;

                Label labelParticipantName = new Label();
                labelParticipantName.Text =
                    participantsTable[i].FirstName_ + "  " + participantsTable[i].LastName_;
                labelParticipantName.FontSize = 20;
                labelParticipantName.VerticalOptions = LayoutOptions.StartAndExpand;
                labelParticipantName.HorizontalOptions = LayoutOptions.StartAndExpand;

                Label labelParticipantPhone = new Label();
                labelParticipantPhone.Text =
                    participantsTable[i].PhoneNumber_;
                labelParticipantPhone.FontSize = 20;
                labelParticipantPhone.VerticalOptions = LayoutOptions.StartAndExpand;
                labelParticipantPhone.HorizontalOptions = LayoutOptions.StartAndExpand;

                Label labelSelect = new Label();
                labelSelect.Text = "Select: ";
                labelSelect.FontSize = 20;
                labelSelect.VerticalOptions = LayoutOptions.Start;
                labelSelect.HorizontalOptions = LayoutOptions.Start;

                CheckBox checkBox = new CheckBox();
                checkBox.VerticalOptions = LayoutOptions.Start;
                checkBox.HorizontalOptions = LayoutOptions.Start;

                StackLayout stackLayout = new StackLayout();
                stackLayout.Orientation = StackOrientation.Horizontal;
                stackLayout.Children.Add(labelSelect);
                stackLayout.Children.Add(checkBox);

                var container = new Grid();

                container.Children.Add(boxView, 0, 0);
                container.Children.Add(labelParticipantName, 0, 0);
                container.Children.Add(labelParticipantPhone, 0, 1);
                container.Children.Add(stackLayout, 0, 2);
                Grid.SetRowSpan(boxView, 3);

                mainStackLayout.Children.Add(container);
            }
}

I do not have idea why, but scrolling is working not smoothly in the app.

https://www.youtube.com/watch?v=x1iEHFPINnE

How to make smooth scrolling?


回答1:


@Lucas Zhang - "Put container in the ScrollView or ListView instead of StackLayout". This recommendation helped to get rid of the scroll jumping.



来源:https://stackoverflow.com/questions/61142429/xamarin-how-to-smoothly-upload-content-to-stacklayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!