Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I'm trying to use AutoSuggestionBox control in WinUI 3, developing in C++. I can't add suggestion to the control. I try handling the TextChanged event to add some text to the subbestion box but I have some runtime error.

Here my code simplified:

void MyWindow::OnAutoSuggestBoxTextChange(IInspectable const& control, AutoSuggestBoxTextChangedEventArgs const& arg)
{
    auto e = control.try_as<AutoSuggestBox>();
    if (!e) return;

    winrt::Windows::Foundation::Collections::IVector<hstring> l = single_threaded_vector<hstring>();
    l.Append(hstring(L"Option 1"));
    l.Append(hstring(L"Option 2"));
    e.ItemsSource(l);
}


The code compile, but fails in the e.ItemsSource(l) with an exception.

Any "suggestion" be appreciated. Thanks.

What I have tried:

I tried with a vector<string>, list<string>, vector<hstring>... nothing work.
Posted

1 solution

This is a bit of a guessing game of what code you have in the rest of your app...

You need to store the 'IVector' object in a member variable of your class, so that it remains valid for the lifetime of the class instance and your error is likely due to the lifetime management of the 'IVector' object. In your code, 'l' is a local variable that goes out of scope after your function 'OnAutoSuggestBoxTextChange' returns. When the 'AutoSuggestBox' control tries to access the items in 'l', it results in an access violation because the object has been destroyed.

You need to add a member variable to your class to store the 'IVector' objejt -
C++
winrt::Windows::Foundation::Collections::IVector<winrt::hstring> m_suggestions


You then need to initialize the 'm_suggestions' vector in your class constructor or some other place where you make a call to it -
C++
MyWindow::MyWindow()
{
    // Initialize the suggestions vector
    m_suggestions = winrt::single_threaded_vector<winrt::hstring>();
    m_suggestions.Append(winrt::hstring(L"Option 1"));
    m_suggestions.Append(winrt::hstring(L"Option 2"));
}


In your 'OnAutoSuggestBoxTextChange' event handler, set your 'ItemsSource' of the 'AutoSuggestBox' to your 'm_suggestions' member variable -

C++
void MyWindow::OnAutoSuggestBoxTextChange(IInspectable const& control, AutoSuggestBoxTextChangedEventArgs const& arg)
{
    auto e = control.try_as<AutoSuggestBox>();
    if (e)
    {
        e.ItemsSource(m_suggestions);
    }
}
 
Share this answer
 
Comments
Vicente Flich 13-Apr-24 13:38pm    
Thaks Andre, I tried so, but still they crash in the same line e.ItemsSource(l) with and unhandled exception ("Interface not compatible").
Andre Oosthuizen 13-Apr-24 14:03pm    
Let me play with this, there is an easy solution, just missing it right now...
Andre Oosthuizen 15-Apr-24 11:36am    
It seems that your 'ItemsSource' expects an object that implements the 'IInspectable' interface, not 'IIterable<iinspectable>'.

Change your class to -
#include <winrt/Windows.Foundation.Collections.h>

struct SuggestionSource : winrt::implements<SuggestionSource, winrt::Windows::Foundation::IInspectable>
{
    winrt::Windows::Foundation::Collections::IVector<winrt::hstring> Suggestions;

    SuggestionSource()
    {
        Suggestions = winrt::single_threaded_vector<winrt::hstring>();
    }

    void AddSuggestion(winrt::hstring suggestion)
    {
        Suggestions.Append(suggestion);
    }
};


This should do the trick.
Vicente Flich 16-Apr-24 4:19am    
Thanks Andre for your support. This seems to be in the right direction, the ItemSource expects an IInspectable, butnow I cant compile because SuggestionSource is an abstract class. I'm thinking implement another control less complicated, maybe a ComboBox...
Vicente Flich 17-Apr-24 4:10am    
I got it! The correct way is an IVector<IInspectable>, not IVector<hstring>:

winrt::Windows::Foundation::Collections::IVector<IInspectable> l = single_threaded_vector<IInspectable>();
l.Append(box_value(L"Opcion1"));
l.Append(box_value(L"Opcion2"));
e.ItemsSource(l);

I put here for anyone with the same problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900