2015-07-19 105 views
0

我有一个window.xaml,它具有不同类型样式的组件(边框颜色为红色,不透明度已更改等)。有一次我想创建一个截图并保存到文件夹。但在此之前,窗口背景应该是透明的,并且应该隐藏someCanvas如何知道何时完成窗口控件样式

我如何知道造型方法何时完成,以便拍摄截图?

public void SomeMethod() 
{ 
    ChangeWindowControlStyles(); 

    //TODO: waint till 'ChangeWindowControlStyles' finished 

    TageScreenshotAndSave(); 
} 

public void ChangeWindowControlStyles() 
{ 
    this.Background.Opacity = 0; 

    this.someCanvas.Visibility = Visibility.Collapsed; 

    //Some other stuff related to window content styling 
} 

public void TakeScreenshotAndSave() 
{ 
    //No multithreading happening here 

    //Just taking screenshot and saving to folder 
} 

EDIT

窗口本身是透明WindowStyle="None",这意味着它不具有边界。在开始时,窗口的Background.Opacity设置为0.1,并且所有的控件都是可见的(除someCanvas之外还有其他控件应始终可见)。

截图之前采取someCanvas隐藏和Background.Opacity设置为0

Window.xaml

<Window 
    WindowStartupLocation="CenterScreen" 
    ResizeMode="NoResize" 
    WindowState="Maximized" 
    WindowStyle="None" 
    AllowsTransparency="True" > 

    <Window.Background> 
     <SolidColorBrush Opacity="0.1" Color="White"/> 
    </Window.Background> 

     <Grid Name="mainGrid" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0"> 

      <!--Main canvas, function holder--> 
      <Canvas Name="canvasAlwaysVisible" Margin="0" Panel.ZIndex="5"> 
       <!-- Controls that are always visible --> 
      </Canvas> 

      <Canvas x:Name="someCanvas" Margin="0" Background="Transparent" Visibility="Visibility"> 
       <!-- Controls with styling --> 
      </Canvas> 

     </Grid> 
</Window> 

EDIT 2

另一件事提的是,内TakeScreenshotAndSave也有System.IO操作 - 获取目录中的所有文件夹,cre ation新目录等。也许.NET看到了这一点,并且它是异步运行的。

+0

只要您不要异步调用'ChangeWindowControlStyles',它将在下一行运行时完成。您不必“等待”返回的方法,只要该行完成并转到下一个方法就立即完成。取决于如何调用SomeMethod,你可能需要调用Dispatcher。 –

+0

您是否试图在没有所有程序窗口或屏幕截图的情况下拍摄屏幕截图,而不仅仅是触发它的窗口,但您还有其他窗口? –

+0

我想隐藏窗口中的一个画布,也要将window.Background Opacity更改为0.所以,我想隐藏特定的控件而不是所有的窗口。我将编辑我的文章,添加一些额外的信息。 @ SaintJob2.0 – Edgar

回答

0

看起来像我找到了解决方案。我不知道它为什么会起作用,需要进一步调查。我在代码示例中提到的TakeScreenshotAndSave方法以某种方式在不同的线程上运行。当在Application.Current.Dispatcher里面包装该方法时,它就起作用了!

public void SomeMethod() 
{ 
    ChangeWindowControlStyles(); 

    var m_dispatcher = Application.Current.Dispatcher; 

    m_dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, 
    new System.Threading.ThreadStart(delegate 
    { 

     TakeScreenshotAndSave(); //Now running on UI thread 

    })); 
} 
+2

它可以工作,因为视图不会立即重新绘制。每次更改后都会有一个通知,重新绘制必须完成,并在应用程序进入空闲状态时完成。所以你在做正确的事情,等待空闲并拍摄截图 –