本文介绍了通过进度栏将数据加载到datagrid match中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止所拥有的:

Here is what i have so far :

        #region Method For Loading Data
    private async Task Loading(Func<string> SearchStringForUser)
    {
        ObservableCollection<VW_Users> collection = new ObservableCollection<VW_Users>();
        object @lock = new object();
        BindingOperations.EnableCollectionSynchronization(collection, @lock);
        DataGrid_User.ItemsSource = collection;
        await Task.Run(() =>
        {


            using (SqlConnection connection = new SqlConnection(PublicVar.ConnectionString))
            {
                SqlCommand command = new SqlCommand("select * From VW_Users where 1 = 1 And @GymID = @GymId", connection);
                command.Parameters.AddWithValue("@GymID", PublicVar.GymID + " " + SearchStringForUser());
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    const int N = 10;
                    VW_Users[] cache = new VW_Users[N];
                    int counter = 0;
                    while (reader.Read())
                    {
                        VW_Users obj = new VW_Users();
                        obj.UserID = Convert.ToInt32(reader["UserID"]);
                        obj.UserName = Convert.ToString(reader["UserName"]);            
                        cache[counter] = obj;
                        //...and so on for each property...

                        if (++counter == N)
                        {
                            //add N items to the source collection
                            foreach (VW_Users x in cache)
                            {
                                collection.Add(x);


                            } 
                            counter = 0;
                            this.Dispatcher.InvokeAsync(() => {
                                MyProg.Value += 20;
                            });

                            ////add a delay so you actually have a chance to see that N items are added at a time
                               System.Threading.Thread.Sleep(500);
                        }
                    }
                    //add any remaining items
                    for (int i = 0; i < counter; ++i)
                    {
                        collection.Add(cache[i]);

                    }

                }
                reader.Close();
            }

        });

    }
    #endregion

正在等待将数据加载到我的datagrid,现在我有两个问题
我如何使图像旋转360度像加载图像...
,我的重要问题是我如何显示进度条$ b $加载的数据b就像当加载10%的数据时,进度栏值获得10%时,进度栏获得100%。我在我的代码中将进度条称为 MyProg,但它不能正常工作。最好的主意是什么?

its awaiting for load data into my datagrid , now i have two qustion right here how i can make a image rotated 360 like loading image...and my important qustion is how i can show the loaded data by Progress barlike when 10% of datas loaded my Progress bar value get 10% when it done my Progress bar get 100% . i callled my Progress bar "MyProg" in my codes, but it dont work nicey. what is best idea for do that?

推荐答案

在您阅读完查询之前,不知道您的查询将返回多少行,因此无法确定在当前实现下您已经处理了10%的行。您可能要使用 ProgressBar ,并且 IsIndeterminate 属性设置为 true

You don't know how many rows your query will return until you have read them so there is no way to know that you have processed 10% given your current implementation. You may want to use a ProgressBar with the IsIndeterminate property set to true though.

正如@bommelding指出的那样,您可能还想考虑使用 SqlDataReader 类的异步API来代替使用 Task.Run

As @bommelding pointed out, you may also want to consider using the asynchronous API of the SqlDataReader class instead of using Task.Run: https://blogs.msdn.microsoft.com/adonet/2012/07/15/using-sqldatareaders-new-async-methods-in-net-4-5-part-2-examples/

给出一个 Image 元素像这样添加到您的XAML标记中:

Given an Image element that you have added to your XAML markup like this:

<Image x:Name="image" Source="image.png" />

..您可以设置其 LayoutTransform 或将 RenderTransform 属性设置为您动画化的 RotateTransform 属性:

..you can set its LayoutTransform or RenderTransform property to a RotateTransform that you animate like this:

RotateTransform rotateTransform = new RotateTransform();
image.RenderTransform = rotateTransform;
image.RenderTransformOrigin = new Point(0.5, 0.5);

rotateTransform.BeginAnimation(RotateTransform.AngleProperty,
    new DoubleAnimation() { From = 0, To = 360, Duration = TimeSpan.FromSeconds(1), RepeatBehavior = RepeatBehavior.Forever, FillBehavior = FillBehavior.Stop });

await Task.Run(...);

rotateTransform.BeginAnimation(RotateTransform.AngleProperty, null);

这篇关于通过进度栏将数据加载到datagrid match中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 03:02