1

也许标题措辞不正确。

只要在 ChildWindow 打开时我不尝试使用它,我就有一个“全局”忙碌指示器,效果很好。

我使用 App.xaml.cs 中的静态方法访问“全局”忙碌指示器:

BusyIndicator b = (BusyIndicator)App.Current.RootVisual;
if (b != null)
{
    b.BusyContent = busyText;
    b.IsBusy = true;
}

但是,如果 ChildWindow 打开,BusyIndi​​cator 总是在它后面。

我以为我可以设置b.Content = VisualTreeHelper.GetOpenPopups().First(),但这也不起作用。

有没有人有关于在打开的 ChildWindows 之上使用 BusyIndi​​cator 的任何提示?

提前致谢。

更新(解决方案)

Dave S 让我走上了正轨。它比我希望的要复杂,但这是我的解决方案。

首先,我必须为 ChildWindow 制作一个完整的样式,复制所有的 Template 样式(由于后期大小的原因而遗漏了一些):

<Style x:Key="MyChildWindowStyle" TargetType="gs:MyChildWindow">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="gs:MyChildWindow">
                <toolkit:BusyIndicator IsBusy="{TemplateBinding IsBusy}" BusyContent="{TemplateBinding BusyContent}" BusyContentTemplate="{StaticResource MyBusyIndicatorDataTemplate}">
                    <Grid>
                        <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </toolkit:BusyIndicator>
</Style>

然后,我创建了我的基类;注意构造函数设置样式。(如果我试图使其抽象,就会发生错误。)

public class MyChildWindow : ChildWindow
{
    public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(MyChildWindow), null);
    public static readonly DependencyProperty BusyContentProperty = DependencyProperty.Register("BusyContent", typeof(object), typeof(MyChildWindow), null);

    public bool IsBusy
    {
        get { return (bool)GetValue(IsBusyProperty); }
        set { SetValue(IsBusyProperty, value); }
    }

    public object BusyContent
    {
        get { return GetValue(BusyContentProperty); }
        set { SetValue(BusyContentProperty, value); }
    }

    public MyChildWindow()
    {
        this.Style = Application.Current.Resources["MyChildWindowStyle"] as Style;
    }
}

确保添加一个新的 ChildWindow,并将 <controls:ChildWindow 更改为 <gs:MyChildWindow(与代码隐藏相同)。

最后,更新静态 SetBusyIndi​​cator 方法:

public static void SetBusyIndicator(string busyText, Uri busyImage)
{
    var op = VisualTreeHelper.GetOpenPopups().Where(o => o.Child is MyChildWindow);
    if (op.Any())
    {
        var bidc = new MyBusyIndicatorDataContext(busyText, busyImage);
        foreach (System.Windows.Controls.Primitives.Popup p in op)
        {
            var c = p.Child as MyChildWindow;
            c.BusyContent = bidc;
            c.IsBusy = true;
        }
    }
    else
    {
        BusyIndicator b = Current.RootVisual as BusyIndicator;
        if (b != null)
        {
            b.BusyContent = new MyBusyIndicatorDataContext(busyText, busyImage);
            b.IsBusy = true;
        }
    }
}

我不确定这是最有效的,但它似乎工作得很好。

4

2 回答 2

3

当您将应用程序根视觉设置为繁忙的微调器时,您将无法更改 Z-Index,因此它高于 ChildWindow。最好的办法是扩展 ChildWindow 控件并在其中添加一个繁忙的微调器,然后在打开窗口时在 ChildWindow 上设置 IsBusy 而不是根视觉。

希望有帮助。

于 2012-02-15T17:40:16.170 回答
1

您不必继承 ChildWindow 就可以在 ChildWindows 上使用 BusyIndi​​cator。我正在使用以下解决方案:

1-为所有 ChildWindow 控件定义全局 ContentTemplate。将 IsBusy 属性绑定到 ChildWindow 的数据上下文的“IsBusy”。

<Style TargetType="sdk:ChildWindow">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <toolkit:BusyIndicator IsBusy="{Binding IsBusy}">
                        <ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </toolkit:BusyIndicator>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

2-定义一个在silverlight应用程序运行期间准备好的单例类。我为此选择了 App 类。实现 INotifyPropertyChanged 接口以自动刷新所有 IsBusy 活页夹。实现 IsBusy 属性。实现 ShowBusyIndi​​cator 和 HideBusyIndi​​cator 方法。在 ShowBusyIndi​​cator 方法中迭代所有打开的 ChildWindows 并更新它们的 DataContexts。

public class App : Application, INotifyPropertyChanged
{
    public static BusyIndicator GlobalBusyIndicator { get; private set; }

    private bool _isBusy;
    public bool IsBusy
    {
        get
        {
            return _isBusy;
        }
        set
        {
            _isBusy = value;
            RaisePropertyChanged(new PropertyChangedEventArgs("IsBusy"));
        }
    }

    public static void ShowBusyIndicator()
    {
        var popups = VisualTreeHelper.GetOpenPopups();
        foreach (var p in popups)
        {
            ChildWindow cw = p.Child as ChildWindow;
            if (cw != null)
                cw.DataContext = App.Current;
        }
        (Current as App).IsBusy = true;
        GlobalBusyIndicator.IsBusy = true;
    }

    public static void HideBusyIndicator()
    {
        (Current as App).IsBusy = false;
        GlobalBusyIndicator.IsBusy = false;
    }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        string baseurl = Host.Source.AbsoluteUri;
        BaseUrl = baseurl.Substring(0, baseurl.IndexOf("ClientBin"));

        GlobalBusyIndicator = new BusyIndicator();
        GlobalBusyIndicator.HorizontalAlignment = HorizontalAlignment.Stretch;
        GlobalBusyIndicator.VerticalAlignment = VerticalAlignment.Stretch;
        GlobalBusyIndicator.Content = new Shell();

        this.RootVisual = GlobalBusyIndicator;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }
}
于 2012-09-12T07:50:15.627 回答