C# 在c中获取窗口集合时尝试排除某些wpf窗口#

C# 在c中获取窗口集合时尝试排除某些wpf窗口#,c#,wpf,C#,Wpf,我编写了一些代码,使用WindowCollection handles=Application.Current.Windows获取WindowCollection 它工作正常,但是我通过按下一个名为SelectScreenShots的窗口中的一个按钮来获取WindowCollection,该窗口从一个名为AskAQuestionDialog的窗口打开。最后,窗口被渲染为位图并显示在SelectScreenShots中。我想你可以猜到用户在最后一个窗口中显示后会做什么 我想从句柄中排除AskAQu

我编写了一些代码,使用
WindowCollection handles=Application.Current.Windows获取
WindowCollection

它工作正常,但是我通过按下一个名为SelectScreenShots的窗口中的一个按钮来获取WindowCollection,该窗口从一个名为AskAQuestionDialog的窗口打开。最后,窗口被渲染为位图并显示在SelectScreenShots中。我想你可以猜到用户在最后一个窗口中显示后会做什么

我想从
句柄中排除AskAQuestionDialog和WindowCollection,最好在渲染和显示它们之前。有人提到使用
Object.ReferenceEquals
来获取和删除它们,但我不确定具体如何实现这一点。以下是它们的收集和呈现方式

public static List<BitmapSource> RenderWindows()
{
    WindowCollection handles = Application.Current.Windows;
    List<BitmapSource> renderWindows = new List<BitmapSource>();
    foreach (Window window in handles)
    {
        double width = window.Width;
        double height = window.Height;
        RenderTargetBitmap bitmap = (new RenderTargetBitmap((int)width, (int)height, 96d, 96d, PixelFormats.Default));
        bitmap.Render(window);
        renderWindows.Add(bitmap);
    }

    return renderWindows;
}
publicstaticlist RenderWindows()
{
WindowCollection句柄=Application.Current.Windows;
List renderWindows=new List();
foreach(控制柄中的窗口)
{
双倍宽度=窗口宽度;
双倍高度=窗户高度;
RenderTargetBitmap位图=(新的RenderTargetBitmap((int)宽度,(int)高度,96d,96d,PixelFormats.Default));
渲染(窗口);
添加(位图);
}
返回渲染窗口;
}

如何删除这两个窗口?

我想说的是,如果您知道某个类型的窗口永远不想包括:

public static List<BitmapSource> RenderWindows()
{
    WindowCollection handles = Application.Current.Windows;
    List<BitmapSource> renderWindows = new List<BitmapSource>();
    foreach (Window window in handles)
    {
        if (window.GetType() == typeof(AskAQuestionDialog))
            continue;

        double width = window.Width;
        double height = window.Height;
        RenderTargetBitmap bitmap = (new RenderTargetBitmap((int)width, (int)height, 96d, 96d, PixelFormats.Default));
        bitmap.Render(window);
        renderWindows.Add(bitmap);
    }

    return renderWindows;
}
publicstaticlist RenderWindows()
{
WindowCollection句柄=Application.Current.Windows;
List renderWindows=new List();
foreach(控制柄中的窗口)
{
if(window.GetType()==typeof(AskAQuestionDialog))
继续;
双倍宽度=窗口宽度;
双倍高度=窗户高度;
RenderTargetBitmap位图=(新的RenderTargetBitmap((int)宽度,(int)高度,96d,96d,PixelFormats.Default));
渲染(窗口);
添加(位图);
}
返回渲染窗口;
}

一些LINQ肯定会看起来更好:

public static List<BitmapSource> RenderWindows()
{
    var windows = Application.Current.Windows
                                     .OfType<Window>()
                                     .Where(x => x.GetType() != typeof(AskAQuestionDialog));

    var bitmaps = new List<BitmapSource>();

    foreach (var window in windows)
    {
        var bitmap = new RenderTargetBitmap((int)window.width, (int)window.height, 96d, 96d, PixelFormats.Default));
        bitmap.Render(window);

        bitmaps.Add(bitmap);
    }

    return bitmaps;
}
publicstaticlist RenderWindows()
{
var windows=Application.Current.windows
第()类
.Where(x=>x.GetType()!=typeof(AskAQuestionDialog));
变量位图=新列表();
foreach(windows中的var窗口)
{
var bitmap=newrenderTargetBitmap((int)window.width,(int)window.height,96d,96d,PixelFormats.Default));
渲染(窗口);
位图。添加(位图);
}
返回位图;
}