简体   繁体   English

设置“可见性”属性不显示控件

[英]Setting Visibility property does not show control

I am having problems with visibility of the spinner control. 我在旋转器控件的可见性方面遇到了问题。 The control itself works... If I set it to visible right after Initialize it shows and animates as expected. 控件本身有效...如果我在初始化之后将其设置为可见,则按预期显示和动画。

But if I try to show it from the code it never gets drawn... 但如果我试图从代码中显示它,它永远不会被绘制......

the .cs file (presenter) .cs文件(演示者)

private void SaveDocument(Document aDocument)
{
  if (AllowFlag != null)
  {
    this.View.ShowDocumentProgressSpinner(true);
    this.Save(aDocument);
    this.View.ShowDocumentProgressSpinner(false);
  }
}

the xaml.cs file xaml.cs文件

void IDocumentView.ShowDocumentProgressSpinner(bool show)
{
  if (show)
  {
    this.DocumentProgressSpinner.Visibility = Visibility.Visible;
  }
  else
  {
    this.DocumentProgressSpinner.Visibility = Visibility.Hidden;
  }
}

If i set the visibility to visible right after initialize the spinner works! 如果我在初始化微调器之后将可见性设置为可见!

part of the xaml of the main control (the spinner is custom control) 主控件的xaml的一部分(微调器是自定义控件)

...
      <Viewbox Grid.Row="3" Width="30" Height="30"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
        <my:DocumentProgressSpinnerView x:Name="DocumentProgressSpinner" />
...

Probably another threading problem, but I have also tried: 可能是另一个线程问题,但我也尝试过:

Thread.CurrentThread == Dispatcher.CurrentDispatcher.Thread

TRUE 真正

Dispatcher.FromThread(Thread.CurrentThread).CheckAccess()

TRUE 真正

The control gets invoked, because the "windows spinner" gets activated, just the control never gets shown... 控件被调用,因为“windows spinner”被激活,只是控件永远不会显示...

The problem is that you are running your save operation on the dispatcher thread and during the save operation the dispatch thread is blocked the whole time. 问题是您在调度程序线程上运行保存操作,并且在保存操作期间,调度线程一直被阻塞。 It's only after your save operation has finished that the UI is updated and thus you will never see the "waiting" state. 只有在您的保存操作完成后,UI才会更新,因此您永远不会看到“等待”状态。 Instead you should spin off a new thread and from within the event dispatch and set the wait indicator to visible. 相反,您应该从事件调度中旋转一个新线程,并将等待指示器设置为可见。 In the separate thread perform the save operation and once the saving is done, use the dispatcher to hide the wait indicator again on the Dispatcher thread. 在单独的线程中执行保存操作,保存完成后,使用调度程序在Dispatcher线程上再次隐藏等待指示符。

See this answer for more details on how to implement this. 有关如何实现此问题的更多详细信息,请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM