1

我有一个使用 Template10 的 UWP。我想要一个带有项目的网格,并且在OnClick一个项目的事件中我想打开另一个页面。通常

var item = (Invitation)e.ClickedItem;
this.Frame.Navigate(typeof(MainPage), item.Id);

似乎不起作用。我怎样才能做到这一点?

4

1 回答 1

1

我制作了一个简单的示例来向您展示如何将参数从一个页面传递到另一个页面。我不会使用 MVVM 架构,因为这将是一个简单的演示。

这是我的主页:

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

如您所见,这里没有什么特别之处。只有启用了单击的列表视图。

下面是代码背后的样子:

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页面。

这是我的 SideView 页面的样子:

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

这就是我背后的代码的样子:

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事件,我们可以得到传递的参数。这是你缺少的部分,所以请注意。希望这有助于解决您的问题。

于 2016-06-22T20:10:06.943 回答