我制作了一个简单的示例来演示如何将参数从一个页面传递到另一个页面。我不会使用 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 事件,我们可以得到传递的参数。
这是你缺少的部分,所以请注意。希望这有助于解决您的问题。