2

我正在开发一个简单的自定义控件,它应该充当菜单系统。它由 2 个按钮组成:返回和主页以及上面的菜单项。ControlTemplate 看起来像这样:

  <ControlTemplate x:Key="FancyColorPickerTemplate">
        <menu:BusyDecorator x:Name="BusyDecorator"  Style="{StaticResource BusyDecoratorStyle}">
            <menu:BusyDecorator.IsBusyIndicatorShowing>
                <PriorityBinding>
                    <Binding Path="IsBusy" RelativeSource="{RelativeSource AncestorType={x:Type CustomControls:Menu}}"/>
                </PriorityBinding>
            </menu:BusyDecorator.IsBusyIndicatorShowing>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.70*"/>
                    <RowDefinition Height="0.30*"/>
                </Grid.RowDefinitions>

                <ItemsControl Grid.Row="0" Name="Part_ItemControl" 
                        ItemTemplateSelector="{StaticResource imgStringTemplateSelector}"
                          HorizontalAlignment="Center" VerticalAlignment="Center">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel Orientation="Horizontal"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
                <Button  Grid.Row="1" Name="PART_BackButton"  FontSize="20" Content="Back" HorizontalAlignment="Left" />
                <Button  Grid.Row="1" Name="PART_HomeButton"  FontSize="20" Content="Home" HorizontalAlignment="Center" />
            </Grid>
        </menu:BusyDecorator>
    </ControlTemplate>

在 ItemsControl 上有一个 ItemTemplateSelector 用于选择显示元素的 DataTemplate(可以是按钮或用户控件)。例子 :

    <DataTemplate x:Key="ButtonTemplate">
        <Grid Margin="10,0,10,0">
            <Button Content="{Binding Title}" ></Button>
          </Grid>
    </DataTemplate>

    <DataTemplate x:Key="UserControlTemplate">
        <Grid Margin="10,0,10,0">
            <CustomControls:ColorPickerUserControl Width="200"   Height="200"/>
        </Grid>
    </DataTemplate>

在 Codebehind 中,如果需要,我检查单击了哪个元素并加载子菜单(通过设置 ItemsControl 的 ItemsSource 属性):

 void Menu_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        ItemsControl itemsControl = GetTemplateChild("Part_ItemControl") as ItemsControl;
        object item = GetElementFromPoint(itemsControl, e.GetPosition(itemsControl));

        if (item != null && item is MenuItem2)
        {
            MenuItem2 mi = item as MenuItem2;
            if (mi.SubMenu != null)
            {
                IsBusy = true;
                Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                 {
                     MenuItems = new ObservableCollection<MenuItem2>(mi.SubMenu.Items);
                     IsBusy = false;
                 }));
                m_MenuStack.Push(mi.SubMenu);
            }
            else
            {
                if (!IsBusy)
                {
                    ExecuteAction(mi.Ac);
                }
            }
        }
        else
            Console.WriteLine("no item found");
    }

上面代码中的 MenuItems 绑定到 ItemsSource 属性,该属性会重新评估 ItemsControl 并根据 DataTemplateSelector 应用相应的 DataTemplate。我的代码中的问题在于上面的 IsBusy 属性,它应该显示 BusyDecorator(参见 xaml),而 DataTemplate 是一个需要很长时间才能显示的 UserControl。它不起作用,因为我猜 UserControl 正在 UI 线程上加载,并且 IsBusy 属性会触发 UI 线程上的操作。

我是否采用了错误的方法?有什么办法可以使这项工作?

4

1 回答 1

0

您始终可以设置繁忙标志并在繁忙集的渲染完成后安排 UI 线程上的其余工作。

这里的神奇之处在于线程使用优先级进行调度,System.Windows.Threading.DispatcherPriority.Render这使得任务与渲染同时运行。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Click="Button_Click_1">LongTask</Button>
        <TextBlock Grid.Row="1" Text="{Binding IsBusy}"/>
    </Grid>
</Window>

和背后的代码

using System;
using System.Threading;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public bool IsBusy
        {
            get { return (bool)GetValue(IsBusyProperty); }
            set { SetValue(IsBusyProperty, value); }
        }

        public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            IsBusy = true;
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
                Thread.Sleep(2000);

                IsBusy = false;
            }), System.Windows.Threading.DispatcherPriority.Render, null);
        }
    }
}
于 2013-04-16T12:43:13.433 回答