【问题标题】:"IsAsync" has no effect for slow property?“IsAsync”对慢属性没有影响?
【发布时间】:2012-10-29 12:38:25
【问题描述】:

我的数据源很慢,所以我异步创建它。此外,我的视图模型的许多属性本身都很慢。因此,我也让它们异步(绑定到 Telerik 属性网格):

<Grid>
  <Grid.DataContext>
    <ObjectDataProvider ObjectType="{x:Type viewModels:MyViewModel}"
      IsAsynchronous="True" x:Name="myViewModel" />
  </Grid.DataContext>
  <telerik:RadPropertyGrid x:Name="settings" Item="{Binding IsAsync=True}">
    <telerik:RadPropertyGrid.PropertyDefinitions>
      <telerik:PropertyDefinition
        Binding="{Binding Path=SlowProperty,IsAsync=True,Mode=TwoWay}" />
    </telerik:RadPropertyGrid.PropertyDefinitions>
  </telerik:RadPropertyGrid>
</Grid>

调试时,我可以看到数据源确实是在另一个线程中创建的,因此不会阻塞 UI。然而,当我进入SlowProperty 时,调试器显示我仍在主线程中——这可以通过 UI 挂起直到属性返回的事实来证明。

问:我在这里缺少什么? IsAsync 对于异步属性还不够吗?

编辑:这似乎适用于普通的TextBlockIsAsync 行为是否依赖于控件的实现? (在这种情况下,我怀疑属性网格中存在错误。)

【问题讨论】:

    标签: wpf mvvm asynchronous viewmodel


    【解决方案1】:

    我们总是鼓励您使用 IsAsync BindingPriorityBinding。 GUI 将等到属性被执行。但是对于PriorityBinding,它将检查第一个Binding 属性是否为slow,如果是,它将选择下一个placeholder 绑定(应该很快)。但是当 slow 属性被求值时,它会通过绑定成为活动值。

    <TextBlock>
     <TextBlock.Text>
      <PriorityBinding FallbackValue="defaultvalue">
        <Binding Path="SlowestProp" IsAsync="True"/>
        <Binding Path="SlowerProp" IsAsync="True"/>
        <Binding Path="SurelyFastProp" />
      </PriorityBinding>
     </TextBlock.Text>
    </TextBlock>    
    

    在您的情况下,您可以简单地设置一些 defaultValue 而不是提供多个绑定。

    【讨论】:

    • 你是说PriorityBinding 对异步绑定至关重要?
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2018-08-10
    • 2021-12-04
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多