[英]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.