C# 按进度条在datagrid中加载数据匹配
以下是我到目前为止的情况:C# 按进度条在datagrid中加载数据匹配,c#,wpf,datagrid,C#,Wpf,Datagrid,以下是我到目前为止的情况: #region Method For Loading Data private async Task Loading(Func<string> SearchStringForUser) { ObservableCollection<VW_Users> collection = new ObservableCollection<VW_Users>(); object @l
#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
正在等待将数据加载到我的数据网格中,现在我有两个问题
如何使图像像加载图像一样旋转360度。。。
我的重要问题是如何通过进度条显示加载的数据
比如,当10%的数据加载我的进度条值时,当加载完成时,我的进度条值为10%,当加载完成时,我的进度条值为100%。我在代码中调用了我的进度条MyProg,但它工作得不好。做那件事最好的办法是什么
我如何通过进度条显示加载的数据,比如当10%的数据加载时,我的进度条值得到10%,当它完成时,我的进度条得到100%
在读取之前,您不知道查询将返回多少行,因此在当前实现的情况下,无法知道您已经处理了10%。不过,您可能希望使用IsIndeterminate属性设置为true的ProgressBar
AS @ MulMeldId指出,您可能还想考虑使用Sql DATaReADER类的异步API,而不是使用Teas.Run:
如何使图像像加载图像一样旋转360度 给定已添加到XAML标记中的图像元素,如下所示:<Image x:Name="image" Source="image.png" />
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);
请参阅。避免任务。运行并睡眠。你不需要它们,它们会咬你。在你读取它们之前,你不知道你的查询将返回多少行,因此根据当前的实现,你无法知道你已经处理了10%。你能帮我了解旋转360*图像吗?就像放置图像动画一样,一切都没问题,希望它在它的位置移动,我只是想知道,我不确定我是否理解你的意思。您是否尝试设置RenderTransform而不是LayoutTransform?我将您的所有代码复制粘贴到我的源代码中,我的图像旋转360度即可。但图像正在向右和向后移动,我只想旋转360度moving@RezaPak:使用RenderTransform并将图像的RenderTransformMorigin属性设置为0.5,0.5。@RezaPak:我刚刚做了。