我有一个应用程序,当用户键入或选择列表框时,屏幕的一部分会动态更新为新视图。

问题是,由于 WPF 在单个线程中运行所有内容,因此视图的显示可能会干扰键入或导航,从而使应用程序响应速度降低。我想做的是在不同的线程中运行视图部分。

我的第一个想法是使用在不同线程上运行的窗口,但不仅仅是一种黑客行为,还有当单击主窗口时窗口失去焦点并被放置在主窗口后面的问题。我可以把它放在最上面,但我还需要在它前面放置其他窗口。

那么实现此目的的最佳方法是什么,我可以将视图放置在框架中并在不同的线程中运行它吗?

没有正确的解决方案

其他提示

可以加载/在后台线程中生成的数据,然后使用更新UI的 Dispatcher.BeginInvoke

我建议您使用您想要显示的这块屏幕的“可见性”属性,并在用户键入或选择时使用触发器将其从“不可见”或“折叠”设置为“可见”。或者,您可以设置不透明度属性的动画以产生很酷的淡入淡出效果;-) 我将添加一些代码来说明这一点。编辑:耗时的后台任务,例如文件操作,可以使用 后台工作者

<Window x:Class="VisibleOnTypingSpike.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 Orientation="Horizontal">
        <Label Name="TypingSnooper"
               Visibility="{Binding TypingSnooperVisibility}">
            You are typing!</Label>
        <Label>
            <Label.Style>
                <Style>
                    <Setter Property="Label.Opacity" Value="0"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasListBoxNewSelection}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation From="0" To="1"
                                                         Duration="0:0:1"
                                                         Storyboard.TargetProperty="Opacity"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation From="1" To="0"
                                                         Duration="0:0:1"
                                                         Storyboard.TargetProperty="Opacity"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
            You selected!
        </Label>
    </StackPanel>
    <TextBox TextChanged="TextBox_TextChanged"></TextBox>
    <ListBox Name="SimpleListBox"
             SelectionChanged="SimpleListBox_SelectionChanged">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
    </ListBox>
</StackPanel>

    using System.Windows;
    using System.Windows.Controls;

namespace VisibleOnTypingSpike
{
    public partial class Window1 : Window
    {
        public Visibility TypingSnooperVisibility
        {
            get { return (Visibility)GetValue(TypingSnooperVisibilityProperty); }
            set { SetValue(TypingSnooperVisibilityProperty, value); }
        }

        public static readonly DependencyProperty TypingSnooperVisibilityProperty =
            DependencyProperty.Register("TypingSnooperVisibility",
                                        typeof(Visibility),
                                        typeof(Window1),
                                        new UIPropertyMetadata(System.Windows.Visibility.Collapsed));

        public bool HasListBoxNewSelection
        {
            get { return (bool)GetValue(HasListBoxNewSelectionProperty); }
            set { SetValue(HasListBoxNewSelectionProperty, value); }
        }

        public static readonly DependencyProperty HasListBoxNewSelectionProperty =
            DependencyProperty.Register("HasListBoxNewSelection",
                                        typeof(bool),
                                        typeof(Window1),
                                        new UIPropertyMetadata(false));

        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textbox = (TextBox) sender;
            if (textbox.Text.Length > 0) TypingSnooperVisibility = Visibility.Visible;
            else TypingSnooperVisibility = Visibility.Hidden;
        }

        private void SimpleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            HasListBoxNewSelection = true;
            HasListBoxNewSelection = false;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top