[英]ContentFrame.Navigate works for navigation items but not AutoSuggestBox
我有一個帶有NavigationView的UWP應用。 我一直在參考該文檔,該文檔包含所有示例的工作示例,但更深入地使用了AutoSuggestBox。 https://docs.microsoft.com/zh-cn/windows/uwp/controls-and-patterns/navigationview
我想嘗試的是在QuerySubmitted上更新MainPage NavigationView的ContentFrame,但我嘗試使用ContentFrame.Navigate進行此操作,但是最終結果是ContentFrame完全空白。 我很困惑,因為關於AutoSuggestBox的信息並不多。
我當前的代碼如下所示:
private async void AutoSuggestBox_QuerySubmittedAsync(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
searchResultsClass.searchQuery = suggestBox.Text;
Debug.WriteLine(searchResultsClass.searchQuery);
await searchResultsClass.SearchAsync();
this.ContentFrame.Navigate(typeof(SearchResults));
}
SearchResults.xaml,與我的其他工作視圖相同:
<Page
x:Class="TestApp.Views.SearchResults"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<GridViewHeaderItem Content="Search results for ''" FontSize="36"/>
</Grid>
</Page>
無法在我這一邊重現您的問題。 ContentFrame.Navigate
可以很好地與QuerySubmitted
事件句柄配合使用。 XAML中的AutoSuggestBox
如下所示:
<NavigationView.AutoSuggestBox>
<AutoSuggestBox x:Name="ASB" QueryIcon="Find" QuerySubmitted="ASB_QuerySubmitted"/>
</NavigationView.AutoSuggestBox>
結果:
因此,請在this.ContentFrame.Navigate(typeof(SearchResults));
上添加一個斷點this.ContentFrame.Navigate(typeof(SearchResults));
代碼行,並調試您的項目以檢查是否可以成功完成此步驟。 如果仍有問題,請上傳一個最小的復制項目。
更新
對於項目測試,問題在於SearchResults
頁面缺少構造方法。 例如:
public SearchResults()
{
this.InitializeComponent();
}
public async Task SearchAsync()
{
...
}
默認情況下,每個導航都會創建所請求的特定Page(或子類)的新實例,並處置前一個頁面實例。 詳細信息請參考Page
類。 因此,在您的代碼段中的MainPage
中創建實例SearchResults
可能毫無意義,並且不會對顯示的導航SearchResults
頁面產生影響。
如果要將AutoSuggestBox
的文本傳遞給另一個頁面,則應使用Navigation方法在頁面之間傳遞信息 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.