使用不工作的tap事件将数据从数据库发送到另一个页面

本文关键字:数据库 另一个 数据 工作 tap 事件 | 更新日期: 2023-09-27 18:03:28

我要做的是,在搜索事件并点击它之后,用户必须被重定向到另一个称为事件页面的页面,其中将显示选中事件的所有详细信息。但它不工作,我得到以下错误

An exception of type 'System.InvalidCastException' occurred but was not handled in user code
Additional information: Unable to cast object of type 'System.Boolean' to type 'NGO_Volunteer_Comm_Platform_v1._0.DataModel.Event'.

我之前确实尝试过类似的东西,但它不能与tap事件

一起工作

以下是我的代码:

xaml:

<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,72,19,0">
    <!--<ScrollViewer>-->
    <ListBox Background="Black"  x:Name="listBox" FontSize="26" Margin="0,10,0,0" LayoutUpdated="listbox_layoutUpdate" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name="txtEventName" TextWrapping="Wrap" Text="{Binding}" Tapped="txtEventName_Tapped" IsTapEnabled="True" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <!--</ScrollViewer>-->
</Grid>

xaml.cs:

private void txtEventName_Tapped(object sender, TappedRoutedEventArgs e)
{
    Frame.Navigate(typeof(EventPage), e.Handled);
    //MessageDialog md = new MessageDialog("open");
    //await md.ShowAsync();
}

用户应该导航到的页面:

protected override void OnNavigatedTo(NavigationEventArgs e)
{   
    var b = (Event)e.Parameter;
    eventid = b.Id;
    txtName.Text = b.EventName;
    txtDate.Text = b.Date.Date.ToString();
    //txtTime.Text = b.StartingTIme.TimeOfDay.ToString();
    txtLocation.Text = b.Location;
    txtDescription.Text = b.Desc;
}

使用不工作的tap事件将数据从数据库发送到另一个页面

Handled是bool属性,表示事件是向下还是向上传播。当您不希望事件传播时,可以将此属性设置为true,表示事件已处理。

你需要发送你的数据,而不是这个bool。

要做到这一点,您应该获得DataContext并将其作为参数发送。

private void txtEventName_Tapped(object sender, TappedRoutedEventArgs e)
{
    Event ev = (sender as TextBlock).DataContext as Event;
    Frame.Navigate(typeof(EventPage), ev);
}
相关文章: