How can I transfer parameter from one page to another?

蹲街弑〆低调 提交于 2019-12-13 02:26:00

问题


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?


回答1:


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.

Here is my MainPage:

<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.

Here is how code behind looks like:

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);
}

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.

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);
}

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.



来源:https://stackoverflow.com/questions/37955612/how-can-i-transfer-parameter-from-one-page-to-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!