【问题标题】:How can I transfer parameter from one page to another?如何将参数从一页传输到另一页?
【发布时间】:2016-06-21 22:28:44
【问题描述】:

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

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

似乎不起作用。我该怎么做?

【问题讨论】:

  • 为什么不起作用?您遇到了什么错误。
  • 如果你直接使用没有 NavigationService 的框架,从技术上讲你不是在使用 Template10,我建议你参考源目录中的示例

标签: uwp template10


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

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多