gpt4 book ai didi

c# - 一个窗口中有 25 个 WPF 日历,打开窗口需要 5 秒

转载 作者:太空狗 更新时间:2023-10-30 00:25:11 24 4
gpt4 key购买 nike

我是 WPF 的新手,正在尝试了解它可能会慢多少。我在 Visual Studio 2010 (.NET 4) 中启动了一个新的 WPF 应用程序,并创建了这个 XAML:

<Window x:Class="CalendarTest1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="1000">
<WrapPanel>
<Calendar />
<Calendar />
<Calendar />
...repeats for a total of 25 calendar objects...
</WrapPanel>
</Window>

当我运行我的应用程序时,无论是否在 IDE 中,窗口都需要 5 秒才能打开。打开后,它会快速重绘(当我调整它的大小时)并且一切看起来都很活泼。

我的 PC 不是最快的:AMD 双核 2.3GHz、2GB RAM、XP 32 位操作系统、板载视频。

我可以放置 25 个按钮,而不是日历,并且加载时间不到 1 秒。

我正在尝试在 MS Outlook 日历的日 View 中创建类似小月历的内容,如下所示:

enter image description here

所以我想我可以使用 WrapPanel 并在调整大小时添加/删除日历控件。我可能不需要 25,但即使有 9 或 12,它也比我想象的要慢(我有一个遗留的 Win32 应用程序可以在不到 1 秒的时间内显示 18 个这样的日历)。

我的问题是:

Calendar 控件是否因为某种设计而变慢 - 要么是糟糕的设计,要么只是不是为这种用途而设计的,或者它只是因为它试图显示大量数据/控件/信息而变慢?

如果我不厌其烦地创建自己的控件,假设我使用了一个好的设计(欢迎提出一般性想法),它会更快吗,或者这只是 WPF 的“典型”?

我可以做些什么来使默认的 Calendar 控件更快地用于此用途?

最佳答案

本身不是答案,但将其添加到 Window.Resources 可将我机器的加载时间减少 50%(现在 25 个日历不到 1 秒)

    <Window.Resources>
<Style TargetType="CalendarDayButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CalendarDayButton">
<ContentPresenter ContentSource="Content" Margin="2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Style TargetType="Calendar">
<Setter Property="CalendarDayButtonStyle" Value="{StaticResource {x:Type CalendarDayButton}}"/>
</Style>
</Window.Resources>

编辑:

此视觉变化不会影响控件的功能。所有日历日项目仍然是 Selectable,您可以绑定(bind) Calendar.SelectedDate 属性。

只是没有视觉指示表明某个项目已被选中,因此点击日期似乎没有任何作用。

只需将其添加到您的 ControlTemplate 中:

                <ControlTemplate TargetType="CalendarDayButton">
<ContentPresenter ContentSource="Content" Margin="2"/>

<ControlTemplate.Triggers>
<!-- Set The FontWeight to "Bold" when Selected -->
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

您可以尝试使用此 ControlTemplate 及其 Triggers 来添加视觉效果。请记住,模板越复杂,加载所需的时间就越长。

关于c# - 一个窗口中有 25 个 WPF 日历,打开窗口需要 5 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18540771/

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