How can I transfer parameter from one page to another?
Asked Answered
G

1

1

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?

Goose answered 21/6, 2016 at 22:28 Comment(2)
Why does it not work? What is the error you're getting.Paderewski
if you directly using frame without NavigationService you aren't technically using Template10, I would suggest you reference the samples in the source directoryGlandulous
A
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.

Aruabea answered 22/6, 2016 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.