gpt4 book ai didi

wpf - 如何在 WPF XAML 中制作加载图形?

转载 作者:行者123 更新时间:2023-12-04 18:00:03 24 4
gpt4 key购买 nike

所以我有一个小的 WPF XAML,它可以获取我的 RSS 提要的标题并将它们放在一个 ListBox 中。

但是,加载大约需要 2 秒。

在数据存在之前,如何在 ListBox 中放置某种 AJAXy 旋转图形?

这是代码:

<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="ExternalColors" Source="http://www.tanguay.info/web/rss" XPath="/"/>
</StackPanel.Resources>
<TextBlock Text="RSS Feeds:"/>
<ListBox Name="lbColor" Height="200" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource ExternalColors}, XPath=//item/title}"/>

<TextBlock Text="You selected this feed:"/>
<TextBox
Text="{Binding ElementName=lbColor, Path=SelectedValue}"
Background="{Binding ElementName=lbColor, Path=SelectedValue}">
</TextBox>
</StackPanel>
</Window>

最佳答案

我的解决方案是在我的数据和 WPF 之间实现一个异步层。这将 WPF 与数据端的任何延迟完全分离,并为我提供了很好的事件和属性来触发和绑定(bind)。

我在 View Model or Presenter Model architecture 之上构建了它.我写过a blog post about the basics还有一篇关于 my approach to asynchronous View Models 的较长文章.

这是微调器的 XAML。在 DataContext它需要执行加载的 View 模型。取决于其State , LoadingIndicator将使自己可见并再次崩溃。

<UserControl x:Class="App.WPF.CustomControls.LoadingIndicator"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="20"
Width="20">
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding State}"
Value="Active">
<Setter Property="Visibility"
Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding State}"
Value="Loading">
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding State}"
Value="Invalid">
<Setter Property="Background"
Value="Red" />
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<UserControl.Triggers>
<EventTrigger RoutedEvent="UserControl.Loaded">
<BeginStoryboard>
<Storyboard TargetName="Rotator"
TargetProperty="Angle">
<DoubleAnimation By="360"
Duration="0:0:2"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</UserControl.Triggers>
<Rectangle Stroke="Aqua"
StrokeThickness="2"
Width="10"
Height="10">
<Rectangle.RenderTransform>
<RotateTransform x:Name="Rotator"
Angle="0"
CenterX="5"
CenterY="5" />
</Rectangle.RenderTransform>
</Rectangle>
</UserControl>

[来源版权所有© 2009 dasz.at OG ;这项工作在 MIT License 下获得许可.]

关于wpf - 如何在 WPF XAML 中制作加载图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/473568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com