简体   繁体   English

ContentFrame.Navigate适用于导航项,但不适用于AutoSuggestBox

[英]ContentFrame.Navigate works for navigation items but not AutoSuggestBox

I have a UWP app with a NavigationView. 我有一个带有NavigationView的UWP应用。 I have been referring to this documentation which has working samples for everything except more in depth usage of the AutoSuggestBox. 我一直在参考该文档,该文档包含所有示例的工作示例,但更深入地使用了AutoSuggestBox。 https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/navigationview https://docs.microsoft.com/zh-cn/windows/uwp/controls-and-patterns/navigationview

What I am trying to attempt is to update the ContentFrame of the MainPage NavigationView on a QuerySubmitted, I've attempted to do this with a ContentFrame.Navigate, however, this just ends up with the ContentFrame going completely blank. 我想尝试的是在QuerySubmitted上更新MainPage NavigationView的ContentFrame,但我尝试使用ContentFrame.Navigate进行此操作,但是最终结果是ContentFrame完全空白。 I am very confused as there is not much out there about AutoSuggestBox to go by that's relevant. 我很困惑,因为关于AutoSuggestBox的信息并不多。

My current code looks like this: 我当前的代码如下所示:

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, which is identical to my other working views: 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>

Cannot reproduce your issue on my side. 无法在我这一边重现您的问题。 ContentFrame.Navigate can work well with QuerySubmitted event handle. ContentFrame.Navigate可以很好地与QuerySubmitted事件句柄配合使用。 The AutoSuggestBox is as follows in XAML: XAML中的AutoSuggestBox如下所示:

<NavigationView.AutoSuggestBox>
    <AutoSuggestBox x:Name="ASB" QueryIcon="Find" QuerySubmitted="ASB_QuerySubmitted"/>
</NavigationView.AutoSuggestBox>

And the result: 结果:

在此处输入图片说明

So that please add a break-point on this.ContentFrame.Navigate(typeof(SearchResults)); 因此,请在this.ContentFrame.Navigate(typeof(SearchResults));上添加一个断点this.ContentFrame.Navigate(typeof(SearchResults)); code line, and to debug your project to check if it can go through successfully to this step . 代码行,并调试您的项目以检查是否可以成功完成此步骤。 If you still have issues please upload a minimal reproduced project. 如果仍有问题,请上传一个最小的复制项目。

Updated 更新

For testing you project, the issue comes to be that the SearchResults page lacks of the constructor method. 对于项目测试,问题在于SearchResults页面缺少构造方法。 For example: 例如:

public SearchResults()
{
    this.InitializeComponent();
}
public async Task SearchAsync()
{
   ...
}

By default, each navigation creates a new instance of the specific Page (or subclass) requested, and disposes the previous page instance. 默认情况下,每个导航都会创建所请求的特定Page(或子类)的新实例,并处置前一个页面实例。 Details please reference Page class. 详细信息请参考Page类。 So that create an instance SearchResults in the MainPage in your code snippet may make no sense and will not have influences with the showed navigated SearchResults page. 因此,在您的代码段中的MainPage中创建实例SearchResults可能毫无意义,并且不会对显示的导航SearchResults页面产生影响。

If you want to pass the text of the AutoSuggestBox to another page you should Pass information between pages by the navigate method. 如果要将AutoSuggestBox的文本传递给另一个页面,则应使用Navigation方法在页面之间传递信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM