-1

In MainWindow.xaml.cs, I try to use WindowHelper to create a new window
var window = WindowHelper.GetWindowForElement(this);

But I recieved the following error
CS0103: The name 'WindowHelper' does not exist in the current context

Should I install a nuget package to solve this?

Wu Xiuheng
  • 53
  • 5
  • Where did you read that this would work? – Tim Roberts Aug 07 '23 at 05:17
  • You are getting this error because you are probably using a name that does not exist in the class, namespace, or scope. Try to set a correct `using` to fix that. Perhaps, `Microsoft.Internal.VisualStudio.PlatformUI` will do the trick. – Denis Kiryanov Aug 07 '23 at 05:22
  • It's just a utility class from WinUI3 Gallery (https://github.com/microsoft/WinUI-Gallery), it's not directly from WinUI3 itself: https://github.com/microsoft/WinUI-Gallery/blob/main/WinUIGallery/Helper/WindowHelper.cs To create a new Window, do `new Window();` – Simon Mourier Aug 07 '23 at 05:41

1 Answers1

1

I guess you mean the WindowHelper class in the WinUI 3 Gallery's sample code, right?

This WindowHelper is not a built-in class or in any library. It's implemented as helper class in the WinUI 3 Gallery project. You can find it here.

You should be able to bring the code in your project and use it.

WindowHelper.cs

//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using WinRT.Interop;

namespace AppUIBasics.Helper
{
    // Helper class to allow the app to find the Window that contains an
    // arbitrary UIElement (GetWindowForElement).  To do this, we keep track
    // of all active Windows.  The app code must call WindowHelper.CreateWindow
    // rather than "new Window" so we can keep track of all the relevant
    // windows.  In the future, we would like to support this in platform APIs.
    public class WindowHelper
    {
        static public Window CreateWindow()
        {
            Window newWindow = new Window();
            TrackWindow(newWindow);
            return newWindow;
        }

        static public void TrackWindow(Window window)
        {
            window.Closed += (sender,args) => {
                _activeWindows.Remove(window);
            };
            _activeWindows.Add(window);
        }

        static public AppWindow GetAppWindow(Window window)
        {
            IntPtr hWnd = WindowNative.GetWindowHandle(window);
            WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
            return AppWindow.GetFromWindowId(wndId);
        }

        static public Window GetWindowForElement(UIElement element)
        {
            if (element.XamlRoot != null)
            {
                foreach (Window window in _activeWindows)
                {
                    if (element.XamlRoot == window.Content.XamlRoot)
                    {
                        return window;
                    }
                }
            }
            return null;
        }

        static public UIElement FindElementByName(UIElement element, string name)
        {
            if (element.XamlRoot != null && element.XamlRoot.Content != null)
            {
                var ele = (element.XamlRoot.Content as FrameworkElement).FindName(name);
                if (ele != null)
                {
                    return ele as UIElement;
                }
            }
            return null;
        }

        static public List<Window> ActiveWindows { get { return _activeWindows; }}

        static private List<Window> _activeWindows = new List<Window>();
    }
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21