如何将参数从一页转移到另一页? [英] How can I transfer parameter from one page to another?
问题描述
我有一个使用Template10的UWP.我想要一个包含项目的网格,并且要在项目的OnClick
事件上打开另一个页面.通常
I have an UWP using Template10. I want to have a grid with items and on the OnClick
event of an item I want to open another page. The usual
var item = (Invitation)e.ClickedItem;
this.Frame.Navigate(typeof(MainPage), item.Id);
似乎不起作用.我该怎么办?
does not seems to work. How can I do this?
推荐答案
我制作了一个简单的示例,向您演示如何将参数从一页传递到另一页.我将不使用MVVM体系结构,因为它将是一个简单的演示.
I made a simple sample to demonstrate you how you can pass parameteres from one page to other. I will not use MVVM architecture because it will be a simple demonstration.
这是我的主页:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="mainWindow"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView
Name="lvDummyData"
IsItemClickEnabled="True"
ItemClick="lvDummyData_ItemClick"
ItemsSource="{Binding ElementName=mainWindow, Path=DummyData}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
如您所见,这里没有什么特别的.仅启用了单击的列表视图.
As you can see there is nothing special here. Only a listview with click enabled.
这是背后的代码:
public ObservableCollection<string> DummyData { get; set; }
public MainPage()
{
List<string> dummyData = new List<string>();
dummyData.Add("test item 1");
dummyData.Add("test item 2");
dummyData.Add("test item 3");
dummyData.Add("test item 4");
dummyData.Add("test item 5");
dummyData.Add("test item 6");
DummyData = new ObservableCollection<string>(dummyData);
this.InitializeComponent();
}
private void lvDummyData_ItemClick(object sender, ItemClickEventArgs e)
{
var selectedData = e.ClickedItem;
this.Frame.Navigate(typeof(SidePage), selectedData);
}
在这里,我有一个可观察的集合,正在用伪数据填充.除此之外,我还有列表视图中的项目单击事件,并将参数传递到我的SideView
页面.
Here I have an observable collection that I am filling with dummy data. Aside from that I have item click event from my listview and passing the parameter to my SideView
page.
这是我的SideView页面的外观:
Here is how my SideView page looks like:
<Page
x:Class="App1.SidePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="sidePage"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Name="txtResultDisplay" />
</Grid>
</Page>
这就是我后面的代码的样子:
And this is how my code behind looks like:
public SidePage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string selectedDummyData = e.Parameter as string;
if (selectedDummyData != null)
{
txtResultDisplay.Text = selectedDummyData;
}
base.OnNavigatedTo(e);
}
在这里,我们有一个OnNavigatedTo
事件,我们可以获取传递的参数.
这是您缺少的部分,请注意.希望这有助于解决您的问题.
Here we have an OnNavigatedTo
event which we can get passed parameteres.
This is the part you are lacking so please pay attention. Hope this helps resolve your problem.
这篇关于如何将参数从一页转移到另一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!