C# 如何在拆分视图内容中加载页面';UWP上的s帧传递参数?
这是我的主页C# 如何在拆分视图内容中加载页面';UWP上的s帧传递参数?,c#,uwp,windows-10-mobile,windows-10-universal,C#,Uwp,Windows 10 Mobile,Windows 10 Universal,这是我的主页 <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://schema
<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="" 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="" 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="" 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="" 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>
<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>
以下是BiblePage on NavigatedTo事件:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
bible = (Bible)e.Parameter;
}
问题是BiblePage的“OnNavigatedTo”事件没有被触发,因此我无法将“bible”变量从MainPage传输到BiblePage
当我在splitview的内容之外执行此过程时,它可以完美地工作
如何在主页的splitview内容传递参数中加载xaml页面?首先,您应该在xaml中使用
x:Name
而不是Name
来清除元素的nama
由于您在.xaml中声明了一个名称,因此您可以在.xaml.cs中通过名称直接访问它,如:
var frame=this.MyFrame;
重要的是,this.Frame
指承载此页面的Frame
,在本例中它是RootFrame
。这里有一些关键点可以帮助您:
- 一页可以放置许多框架李>
- 任何框架都可以用于导航到新页面,如下所示:
MyFrame.navigate(typeof(NewPage),null)代码>
- 所以一页可以在一页之内李>
请执行以下测试//刚刚测试,没有出现任何问题
1/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="" 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="" 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="" 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="" 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>
<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>
我认为问题出在你的圣经页上,因为代码应该可以正常工作
我根据您的代码示例创建了以下GitHub repo:
如果克隆代码并运行应用程序,您可以看到执行了OnNavigatedTo,并将参数正确传递到第二个页面。这在Windows 10 Mobile Emulator和本地计算机上都有效
要从代码中检查的几件事:
确保使用空白页面模板创建BiblePage
确保BiblePage的XAML包含x:Class属性
实际上,我在BiblePage的构造函数上执行了一段代码,该代码试图在“OnNavigatedTo”事件实例化变量之前使用作为参数传递的变量
这样,在触发“OnNavigatedTo”事件之前,我在其他函数上遇到了一个错误
非常感谢。MyFrame是页面的宿主。如果你使用MyFrame.Navigate,它会工作吗?不,不会。事实上,这是以前的情况。为了让它正常工作,我做了一些改变。两种方式都会加载页面,但不会触发“OnNavigatedTo”事件。我明白你的意思,但我真正需要的是访问在另一个xaml页面中创建的对象,该页面将加载到MyFrame中。当我在“MyFrame.Navigate”中将对象作为参数传递时,我无法访问它,因为未触发加载的xaml的“OnNavigatedTo”事件。如果确实导航到新页面,则应在新页面中调用“OnNavigatedTo”方法。请确保您实际导航到新页面(您是否从UI中看到它?)。当我执行“MyFrame.navigate(typeof(View.BiblePage),bible)”时,它将导航到“BiblePage”,我在屏幕上实际看到它,但BiblePage的“OnNavigatedTo”事件不会被触发,我不知道原因。如果我在splitview内容外点击按钮,它会工作得很好。当我点击“MyFrame.Navigate(typeof(View.BiblePage),BiblePage)”时,它会导航到“BiblePage”,我实际上会在屏幕上看到它,但BiblePage的“OnNavigatedTo”事件不会被触发,我不知道为什么。如果我在splitview内容之外的按钮单击事件中执行相同的操作,则效果会非常好。我看到了新的屏幕,它的“OnNavigatedTo”事件被触发,允许我访问刚才传递的e.参数。我发现,如果我作为参数传递单个字符串或单个整数,它就可以正常工作。但如果我将“bible”对象作为参数传递,“OnNavigatedTo”事件不会被触发。在BiblePage上,我声明了一个对象,该对象的类型和名称与接收导航参数的类型和名称相同。在尝试修复这个问题大约一个小时后,#1完美地解决了它!我使用的是“空白视图”模板。谢谢
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);
}
}
}