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 -
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 -
MyWindow::MyWindow()
{
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 -
void MyWindow::OnAutoSuggestBoxTextChange(IInspectable const& control, AutoSuggestBoxTextChangedEventArgs const& arg)
{
auto e = control.try_as<AutoSuggestBox>();
if (e)
{
e.ItemsSource(m_suggestions);
}
}