InputPane.GetForCurrentView throws COMException (0x80070490): Element not found

Peterjc 86 Reputation points
2023-04-12T22:39:54.0933333+00:00

Hi, I have the following code where I wanted to hook into the onscreen keyboard showing/hiding events when my WinUI3 or WPF application are running on a tablet (in tablet mode) I use the following.

InputPane inputPane = InputPane.GetForCurrentView();
if (inputPane == null)
{
  m_logger.Info("No InputPane found. Keyboard events not hooked up");
  return;
}

inputPane.Showing += InputPane_Showing;
inputPane.Hiding += InputPane_Hiding;

However the call to InputPane inputPane = InputPane.GetForCurrentView(); is giving me

System.Runtime.InteropServices.COMException (0x80070490): Element not found. (0x80070490)
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at WinRT.ExceptionHelpers.ThrowExceptionForHR(Int32 hr)
   at ABI.Windows.UI.ViewManagement.IInputPaneStaticsMethods.GetForCurrentView(IObjectReference _obj)
   at Windows.UI.ViewManagement.InputPane.GetForCurrentView()
   at MyAppWpf.MainWindow.HookupSoftKeyboardEvents()

I use to use similar in UWP applications, and I am sure (but now have no proof) this use to work at least in my WinUI3 application. I am calling this from the Activated event...


    /// <summary>
    /// Window activated event handler
    /// </summary>  
    private void OnActivated(object? sender, EventArgs e)
    {
      if (m_activated)
        return;

      try
      {
        m_activated = true;
        HookupSoftKeyboardEvents();
      }
      catch (Exception ex)
      {
        m_logger.Error(ex.ToString());
      }
    }

For version of the tablet via Environment.OSVersion.VersionString is Microsoft Windows NT 10.0.19045.0 Any ideas why I would get this exception, should the above still work? Thanks in advance

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,926 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.