简体   繁体   中英

C# UWP Execute action in other page

I have a problem with the develop of my UWP app. I want to when button 1 on page 1 is pressed: Page 2 is opened and a sqlite connection is made, then it show the data in a list view.

Currently I have a button on page 2 that connects to sqlite and it fills the list view but as I say it should be do it by button 1 on page 1.

Button Show List View:

private void buttonShowList_Click(object sender, RoutedEventArgs e)
        {
            SQLiteConnection dbConnection = new SQLiteConnection("Database.db");
            string sSQL1 = @"CREATE TABLE IF NOT EXISTS Metrados_Head
                                (ID INTEGER PRIMARY KEY Autoincrement NOT NULL
                                , A VARCHAR ( 255 )
                                , B VARCHAR ( 255 )                               
                                );";

            ISQLiteStatement cnStatement_Head1 = dbConnection.Prepare(sSQL1);
            cnStatement_Head1.Step();
            string sSQL2 = @"INSERT OR REPLACE INTO Metrados_Head ([ID], [A], [B], [C], [D], [E], [F]) VALUES (1,'1','2','3','4','5','6');";
            ISQLiteStatement cnStatement_Head2 = dbConnection.Prepare(sSQL2);
            cnStatement_Head2.Step();
            var datos_Head = new List<Metrados_Head>();
            string sSQL3 = @"SELECT
                            [A],
                            [B],
                            FROM Metrados_Head";

            ISQLiteStatement dbState_Head1 = dbConnection.Prepare(sSQL3);

            while (dbState_Head1.Step() == SQLiteResult.ROW)
            {
                string sA = dbState_Head1["A"] as string;
                string sB = dbState_Head1["B"] as string;

                Metrados_Head Datos_Metrados_Head = new Metrados_Head() { A = sA, B = sB};
                datos_Head.Add(Datos_Metrados_Head);
            }
            ListMetrados_Head.ItemsSource = datos_Head;
}

My Xaml of page 2:

<Button x:Name="buttonShowList" Content="ShowList" HorizontalAlignment="Left" Margin="228,30,0,0" VerticalAlignment="Top" Visibility="Visible" Click="buttonShowList_Click" />
        <ListView x:Name="ListMetrados_Head" HorizontalAlignment="Left" Height="600" Margin="10,109,0,0" VerticalAlignment="Top" Width="1260" BorderBrush="Black" BorderThickness="1" Background="White">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <Grid Width="{Binding ElementName=ListMetrados_Head, Path=ActualWidth}" Padding="0" Margin="0" >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150" />
                        <ColumnDefinition Width="150" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40"/>
                    </Grid.RowDefinitions>
                    <TextBlock x:Name="TextBlock_A" Grid.Column="0" Text="{Binding Path=A}" TextWrapping="Wrap" />
                    <TextBox x:Name="TextBox_B" Grid.Column="1" Text="{Binding Path=B}" TextWrapping="Wrap" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
        </ListView>

In my MainPage I have a Pane (Hamburguer Menu) with a page 2 radiobutton: (Portada is Page 1 and Metrados is Page 2):

 private void HamburgerButtom_Click(object sender, RoutedEventArgs e)
        {
            MenuHamburgesa.IsPaneOpen = !MenuHamburgesa.IsPaneOpen;
            HamburgerButtom.IsChecked = false;
        }

        private void GoBack_Click(object sender, RoutedEventArgs e)
        {
            GoBack.IsChecked = false;
            if (ContentFrame.CanGoBack)
            {
                ContentFrame.GoBack();
            }
        }

        private void NavigateToPortada_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(typeof(PagePortada));
            NavigateToPortada.IsChecked = true;
        }

        private void NavigateToMetrados_Click(object sender, RoutedEventArgs e)
        {
            ContentFrame.Navigate(typeof(PageMetrados));
            NavigateToMetrados.IsChecked = true;            
        }

The question is how I can do this only with button 1 in page 1?

As one way, you can pass some special parameter when you navigating from page1 to page2, for example:

private void GoToConnectPage_OnClick(object sender, RoutedEventArgs e)
{
    Frame.Navigate(typeof(ConnectPage), "connect");
}

And then check this parameter in OnNavigatedTo method on second page:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    var parameter = e.Parameter as string;

    if (parameter != null && parameter.Equals("connect"))
    {
        // Connect here
    }

    base.OnNavigatedTo(e);
}

If I'm not mistaken, you can pass any object as a parameter, not only string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM