4

这是我的 MainPage.xaml

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BibleApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Domain="using:BibleApp.Domain"    
x:Class="BibleApp.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <RelativePanel>
            <Button Name="HamburgerButton" 
                    FontFamily="Segoe MDL2 Assets" 
                    Content="&#xE14C;" FontSize="14" 
                    Click="HamburgerButton_Click"/>
        </RelativePanel>

        <SplitView Name="MySplitView" 
                   Grid.Row="1" 
                   DisplayMode="CompactOverlay" 
                   OpenPaneLength="200" 
                   CompactPaneLength="34" 
                   HorizontalAlignment="Left">
            <SplitView.Pane>
                <ListBox Name="IconsListBox" SelectionMode="Single" SelectionChanged="IconsListBox_SelectionChanged">
                    <ListBoxItem Name="Biblia">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE128;" FontSize="18"/>
                            <TextBlock Text="Bíblia" FontSize="12" Margin="15,0,0,0"/>
                        </StackPanel>
                    </ListBoxItem>

                    <ListBoxItem Name="PesquisarPalavraChave">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE11A;" FontSize="18"/>
                            <TextBlock Text="Pesquisar palavra chave" FontSize="12" Margin="15,0,0,0"/>
                        </StackPanel>
                    </ListBoxItem>

                    <ListBoxItem Name="PesquisarAssunto">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE773;" FontSize="18"/>
                            <TextBlock Text="Pesquisar assunto" FontSize="12" Margin="15,0,0,0"/>
                        </StackPanel>
                    </ListBoxItem>
                </ListBox>
            </SplitView.Pane>
            <SplitView.Content>
                <Frame Name="MyFrame"/>
            </SplitView.Content>
        </SplitView>
    </Grid>
</Page>

这是MainPage背后的代码:

private void IconsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MyFrame = this.Frame;
    if (Biblia.IsSelected) { Frame.Navigate(typeof(View.BiblePage), bible);}
    else if (PesquisarPalavraChave.IsSelected) {Frame.Navigate(typeof(View.SearchWordPage));}
    else if (PesquisarAssunto.IsSelected) { Frame.Navigate(typeof(View.SearchMatterPage)); }    
}

这是 BiblePage OnNavigatedTo 事件:

protected override void OnNavigatedTo(NavigationEventArgs e)
{            
    bible = (Bible)e.Parameter;
}

问题是没有触发 BiblePage 的“OnNavigatedTo”事件,因此我无法将“圣经”变量从 MainPage 传输到 BiblePage。

当我在拆分视图的内容之外执行此过程时,它可以完美运行。

如何在 MainPage 的 splitview 内容传递参数中加载 xaml 页面?

4

4 回答 4

4

请执行以下测试 // 刚刚测试并没有出现任何问题。

1/ MainPage.xaml

<Page
x:Class="App3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App3"
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}">
    <SplitView DisplayMode="Inline" Background="Black" IsPaneOpen="True" OpenPaneLength="360" >
        <SplitView.Pane>
            <Button Content="GoToPage" Click="GoToPage_Click" Foreground="White"/>
        </SplitView.Pane>
        <SplitView.Content>
            <Frame x:Name="contentFrame"/>
        </SplitView.Content>
    </SplitView>
</Grid>
</Page>

2/ MainPage.xaml.cs

namespace App3
{
/// <summary>
/// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void GoToPage_Click(object sender, RoutedEventArgs e)
    {
        contentFrame.Navigate(typeof(SecondPage), new Params() { MyProperty = 42 });
    }
}

public class Params
{
    public int MyProperty { get; set; }
}
}

3/ SecondPage.xaml.cs

namespace App3
{

public sealed partial class SecondPage : Page
{
    public SecondPage()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Params result = (Params)e.Parameter;
        base.OnNavigatedTo(e);  
    }
}
}
于 2015-12-14T18:27:32.933 回答
3

我认为问题出在您的 BiblePage 中,因为代码应该可以正常工作。

我根据您的代码示例创建了以下 GitHub 存储库:https ://github.com/mikoskinen/uwp-frame-datapassing 。

如果您克隆代码并运行应用程序,您可以看到OnNavigatedTo已执行,并且参数正确传递给 SecondPage。这适用于 Windows 10 移动模拟器和本地计算机。

调试器

从您的代码中检查几件事:

  1. 确保使用空白页面模板创建圣经页面
  2. 确保您的 BiblePage 的 XAML 包含x:Class属性。
于 2015-12-14T18:48:46.120 回答
2

首先,您应该使用x:Name而不是Name在您的 XAML 中来清除元素的名称。

由于您在 .xaml 中清除了一个名称,因此您可以在 .xaml.cs 中通过它的名称直接访问它,例如: var frame=this.MyFrame;

重要的是,this.Frame指的Frame是承载 thispage的,在本例中是RootFrame. 所以这里有一些关键点可以帮助你:

  • 一个页面可以放置多个框架;
  • 任何框架都可用于导航到新页面,如下所示:MyFrame.Navigate(typeof(NewPage),null);
  • 所以一个页面可以在一个页面内;
于 2015-12-14T18:11:50.247 回答
1

实际上,我在 BiblePage 的构造函数上执行了一段代码,它试图在“OnNavigatedTo”事件实例化之前使用作为参数传递的变量。

这样,在触发“OnNavigatedTo”事件之前,我在其他功能上遇到了错误。

非常感谢。

于 2015-12-14T19:35:55.350 回答