0

I am using WinUI3 on C++ windows app. And I have to pass TextGetOptions to TextDocument().GetText() according to the document and visual studio intellisense.

void MainWindow::TextPreviewKeyDown(IInspectable const& sender, KeyRoutedEventArgs const& e)
{
    RichEditBox richEditBox = sender.as<RichEditBox>();
    hstring OldText;
    richEditBox.TextDocument().GetText(TextGetOptions::None, OldText);//First Argument
}

But the intellesense shows error for TextGetOptions::None. It says: "Argument for winrt::Windows::UI::Text::TextGetOptions is incompatible with the argument of const winrt::Microsoft::UI::Text::TextGetOptions &"

Compiler error: C2664 cannot convert argument 1 from winrt::Windows::UI::Text::TextGetOptions to const winrt::Microsoft::UI::Text::TextGetOptions &.

Here is the method of GetText()

void GetText(TextGetOptions const& options, [Out] winrt::hstring const& & value);

I have been using WinUI3 with C# for a long time but I am not familiar with WinUI3 in C++. I only know that "&" means the pointer of options. And the answer from Bing Copilot is simply the same as what I did. How can I solve this?

7
  • Do you get a compiler error? If so, what is it? Commented Dec 8, 2024 at 10:46
  • The compiler error is C2664 cannot convert argument 1 fromwinrt::Windows::UI::Text::TextGetOptions to const winrt::Microsoft::UI::Text::TextGetOptions & . Just like the error from visual studio intellisense. Commented Dec 8, 2024 at 10:57
  • Please add the compiler error to the question. Commented Dec 8, 2024 at 11:08
  • I have already put the compiler error on the question, Thank you Commented Dec 8, 2024 at 11:13
  • The Windows::UI::Text::TextGetOptions (WinRT, starts in Windows namespace) type is just not the same as as the Microsoft::UI::Text::TextGetOptions (WinUI3, starts in Microsoft namespace) type, use Microsoft::UI::Text::TextGetOptions::None with full namespace instead Commented Dec 8, 2024 at 11:20

1 Answer 1

2

The issue here is that TextGetOptions is declared in two different namespaces: winrt::Windows::UI::Text and winrt::Microsoft::UI::Text. As such, the types are assumed to be different by the compiler, and a conversion from one to the other isn't available.

To fix the issue you'll have to either

  • drop the using statement that merges winrt::Windows::UI::Text into the local namespace, or
  • use a fully qualified type name (e.g., winrt::Microsoft::UI::Text::TextGetOptions::None in place of TextGetOptions::None).

A bit of background information: Windows 8 introduced a new UI platform (that remains without a name to this day). It shipped as part of the operating system, and its API was exposed in a namespace starting with Windows.

The UI platform was later split out from the OS and moved into its own library called "WinUI 2". Since WinUI 2 was no longer part of the OS its API also moved from a namespace starting with Windows to a namespace beginning with Microsoft. The "old" API under the Windows namespace is still available (and supported) causing frequent head-scratching and pain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.