Xamarin ActivityIndicator因绑定而冻结

Xamarin ActivityIndicator因绑定而冻结,xamarin,xamarin.forms,activity-indicator,Xamarin,Xamarin.forms,Activity Indicator,我的活动指示器有问题。应用程序正在另一个线程中从REST服务器收集数据,并将其绑定到carousel视图。ActivityIndicator工作正常,但当我执行Device.beginInvokeMainThread(()=>BindingContext=ViewModel)时 ActivityIndicator冻结,直到绑定无法完成(通常为2-3秒)。 有什么办法可以避免这个问题吗? 代码: await Task.Run(async () => { DateTime startT

我的活动指示器有问题。应用程序正在另一个线程中从REST服务器收集数据,并将其绑定到carousel视图。ActivityIndicator工作正常,但当我执行
Device.beginInvokeMainThread(()=>BindingContext=ViewModel)时
ActivityIndicator冻结,直到绑定无法完成(通常为2-3秒)。
有什么办法可以避免这个问题吗?

代码:

await Task.Run(async () =>
{
    DateTime startTime = DateTime.Now;
    try
    {
        selectedLocation = App.Session.DataProvider.GetLocation(selectedLocation.IdLocation, true);
        List<OpLocationEquipment> locationEq = App.Session.DataProvider.GetLocationEquipment(new long[] { selectedLocation.IdLocation });
        Logger.Logger.Log(Logger.EventID.Views.ItemDetailsPage.SelectedLocationInfo, Logger.Logger.Params(selectedLocation.IdLocation, selectedLocation.ToString()));

        ViewModel = new LocationDetailsViewModel(selectedLocation, locationEq);
        BindingContext = ViewModel; 
    }
    catch (Exception ex)
    {
        await DisplayAlert(ResourceWrapper.GetValue(Constants.StringKey.ApplicationName), ResourceWrapper.GetValue(Constants.StringKey.UnexpectedErrorOccurredDuringConnectingToServer), ResourceWrapper.GetValue(Constants.StringKey.OK));
        Logger.Logger.Log(EventID.Views.LoginPage.Exception, ex);
    }
    TimeSpan totalTime = DateTime.Now - startTime;
    Logger.Logger.Log(EventID.Views.ItemDetailsPage.TotalTime, Logger.Logger.Params(totalTime.TotalSeconds));
});
HideActivityIndicator();
等待任务。运行(异步()=> { DateTime startTime=DateTime.Now; 尝试 { selectedLocation=App.Session.DataProvider.GetLocation(selectedLocation.IdLocation,true); List locationEq=App.Session.DataProvider.GetLocationEquipment(新的long[]{selectedLocation.IdLocation}); Logger.Logger.Log(Logger.EventID.Views.ItemDetailsPage.SelectedLocationInfo,Logger.Logger.Params(selectedLocation.IdLocation,selectedLocation.ToString()); ViewModel=新位置详情ViewModel(selectedLocation,locationEq); BindingContext=ViewModel; } 捕获(例外情况除外) { 等待DisplayAlert(ResourceWrapper.GetValue(Constants.StringKey.ApplicationName)、ResourceWrapper.GetValue(Constants.StringKey.UnexpectedErrorCurredDuringConnectionToServer)、ResourceWrapper.GetValue(Constants.StringKey.OK)); Logger.Logger.Log(EventID.Views.LoginPage.Exception,ex); } TimeSpan totalTime=DateTime.Now-startTime; Logger.Logger.Log(EventID.Views.ItemDetailsPage.TotalTime,Logger.Logger.Params(TotalTime.TotalSeconds)); }); HideActivityIndicator();
数据模板:

<DataTemplate  x:Key="LocationTemplate">
    <StackLayout>
        <Grid VerticalOptions="FillAndExpand" Padding="0" Margin="0,0,0,-10">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <RelativeLayout Grid.Row="0" HorizontalOptions="FillAndExpand">
                <ffimageloading:CachedImage Source="{Binding BackgroundImage}" Opacity="1" Aspect="Fill" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
                <ScrollView RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width}" RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height}">
                    <StackLayout>
                        <StackLayout Padding="15" BackgroundColor="White">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="auto"/>
                                    <ColumnDefinition Width="auto"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="1" />
                                    <RowDefinition Height="64"/>
                                </Grid.RowDefinitions>
                                <BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BackgroundColor="{StaticResource CompanyColor}" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
                                <StackLayout Grid.Column="0" Grid.Row="0" Grid.RowSpan="2">
                                    <Label TextColor="{StaticResource CompanyColor}" FontSize="12" Text="{Binding LocationAddress}" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center" />
                                </StackLayout>
                                <Grid Grid.Row="1" Grid.Column="1">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Label Grid.Row="0" Text="Fuel status" TextColor="{StaticResource CompanyColor}" FontSize="7" HorizontalOptions="Center" HorizontalTextAlignment="Center"/>
                                    <Label Text="{Binding FuelStatus}" Grid.Row="1" TextColor="{StaticResource CompanyColor}" FontSize="28" HorizontalOptions="Center" HorizontalTextAlignment="Center" />
                                </Grid>
                                <Grid Grid.Row="1" Grid.Column="2">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="auto"/>
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Label Grid.Row="0" Text="Device status" TextColor="{StaticResource CompanyColor}" FontSize="7" HorizontalOptions="Center" HorizontalTextAlignment="Center" />

                                    <ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Start" DownsampleToViewSize="true"
                                    Aspect="AspectFit" WidthRequest="32"  HeightRequest="32" Grid.Row="1" Source="{Binding DeviceStatusImage}">
                                    </ffimageloading:CachedImage>
                                </Grid>
                            </Grid>
                        </StackLayout>
                        <ListView
                                      ItemsSource="{Binding Values}"
                                      VerticalOptions="FillAndExpand"
                                      HasUnevenRows="false"
                                      BackgroundColor="Transparent"
                                      CachingStrategy="RecycleElement"
                                      HeightRequest="{Binding ListViewHeight}"
                                      Margin="10,0,10,0"
                                      SeparatorColor="White">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Grid Padding="15,0,15,15" Margin="0,0,0,0" HeightRequest="36">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="auto" />
                                                <ColumnDefinition Width="5*"/>
                                            </Grid.ColumnDefinitions>
                                            <Label Text="{Binding Descr}" Style="{DynamicResource ListItemTextStyle}" Grid.Column="0" VerticalOptions="CenterAndExpand" FontSize="12"/>
                                            <Label Text="{Binding Value}" Style="{DynamicResource ListItemTextStyle}" Grid.Column="1" VerticalOptions="CenterAndExpand" FontSize="12" HorizontalTextAlignment="End" />
                                        </Grid>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                        <StackLayout Padding="15" BackgroundColor="White">
                            <Label x:Name="lPlotTitle" TextColor="{StaticResource Primary}" VerticalOptions="CenterAndExpand" FontSize="11"/>
                            <Grid>
                                <oxy:PlotView x:Name="plotView" Model="{Binding PlotModel}" VerticalOptions = "Fill" HorizontalOptions = "Fill" WidthRequest="{Binding PlotWidth}" HeightRequest="350"/>
                            </Grid>
                        </StackLayout>
                    </StackLayout>
                </ScrollView>
            </RelativeLayout>
        </Grid>
    </StackLayout>
</DataTemplate>

设置绑定上下文本身不会更新任何UI元素,因此无需将其放入InvokeOnMainThread调用中


如果您消除了这一点,结巴应该会消失。

设置绑定上下文本身不会更新任何UI元素,因此无需将其放入InvokeOnMainThread调用中


如果你摆脱了它,口吃就会消失。

不要在
设备中这样做。beginInvokeMainThread(()=>)将在UI线程上执行它,导致ActivityIndicator挂起。我认为我必须在主线程中进行数据绑定。我错了吗?请看我的答案:)不要在
设备中执行此操作。BeginInvokeMainThread(()=>)将在UI线程上执行它,导致ActivityIndicator挂起。我认为我必须在主线程中进行数据绑定。我错了吗?请看我的答案:)我已经尝试过了,但是我得到了TargetInvocationException:异常已被调用的目标抛出。-->Java.Lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序。为什么要在任务中运行整个过程。run?GetLocation(selectedLocation.IdleLocation,true)和GetLocationEquipment(new long[]{selectedLocation.IdleLocation})正在从REST服务器收集数据,有时需要很多时间。这就是我在任务中运行它的原因。runI已尝试过此操作,但我得到TargetInvocationException:调用的目标已抛出异常。-->Java.Lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序。为什么要在任务中运行整个过程。run?GetLocation(selectedLocation.IdleLocation,true)和GetLocationEquipment(new long[]{selectedLocation.IdleLocation})正在从REST服务器收集数据,有时需要很多时间。这就是我在任务中运行它的原因