Accessing Mainview from another class

Asked

Viewed 67 times

0

Hello, here is an excerpt from the code of the main class, in it I basically mount and first "Page" with a menu in the footer (I left only a menu button to not get huge the post)

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Boletim"
             x:Class="Boletim.MainPage">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>


        <!-- PANEL 1 -->

        <ContentView
            Grid.Row="0"
            IsVisible="True" BackgroundColor="White" x:Name="MainView">


        </ContentView>


        <Grid ColumnSpacing="0" RowSpacing="0" Grid.Row="1" BackgroundColor="White">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            // Aqui tem um menu inferior que pra eu mudar de pagina
            <StackLayout Orientation="Vertical"  HorizontalOptions="Center" Padding="20" Margin="-20,-10,-20,-17" Grid.Column="0">
                <StackLayout.GestureRecognizers>
                    <TapGestureRecognizer Tapped="ClickTap2" NumberOfTapsRequired="1" />
                </StackLayout.GestureRecognizers>
                <Image x:Name="tab_home_icon" IsVisible="True" Source="ic_home_v.png" HeightRequest="22" WidthRequest="22" Margin="0,0,0,-2" />
                <Label Margin="0" x:Name="tab_home" Text="HOME" FontSize="Micro" TextColor="Firebrick"/>
            </StackLayout>

        </Grid>

    </Grid>
</ContentPage>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Boletim
{
    public partial class MainPage : ContentPage
    {

        public MainPage()
        {
            InitializeComponent();

            var page = new Page1();

            page.MostraDados();

            MainView.Content = page.Content;
        }


        public void ClickTap2(object sender, EventArgs e)
        {
            var page = new Page3();
            MainView.Content = page.Content;

            //Icones
            tab_home_icon.Source = "ic_home_v.png";
            tab_calendar_icon.Source = "ic_calendar.png";
            tab_map_icon.Source = "ic_map.png";
            tab_info_icon.Source = "ic_info.png";

            //label
            tab_home.TextColor = Color.Firebrick;
            tab_calendar.TextColor = Color.Black;
            tab_map.TextColor = Color.Black;
            tab_info.TextColor = Color.Black;


        }

    }
}

When going to Page3 it shows a listview and when I select an item it would have to go to another "page" but nothing happens.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Boletim.Page3">
    <ContentPage.Content>
        <ScrollView Orientation="Vertical">
            <StackLayout>
                <Image Source="http://via.placeholder.com/1800x250"/>
                <SearchBar 
                    TextChanged="SearchBar_TextChanged"
                    Placeholder="Busca..." >

                </SearchBar>
                <ListView 
                    x:Name="lista_mp3" 
                    HasUnevenRows="True"  
                    IsPullToRefreshEnabled="True"
                    Refreshing="lista_mp3_Refreshing">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="45"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                            <RowDefinition />
                                            <RowDefinition Height="Auto"/>
                                    </Grid.RowDefinitions>
                                    <Label Grid.Column="0" Text="{Binding descricao}" FontSize="20" />
                                    <Entry IsVisible="False" x:Name="arquivo" Grid.Column="1" Text="{Binding nome_arquivo}"/>
                                    <Button Grid.Column="1" Image="ic_action_play_arrow" Clicked="Button_Clicked" />
                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>


using Boletim.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Boletim
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page3 : ContentPage
    {
        public Page3()
        {
            InitializeComponent();

            MostraDados();
        }


        private async void Button_Clicked(object sender, EventArgs e)
        {
           await Navigation.PushAsync(new Page5());

        }
    }
}

The Button_clicked simply happens.

That navigation I’d like to run. I think I would have to access Contentview from Mainpage, but I tried not to.

  • And where you urged Page3? To work she should be in a NavigationPage and in the same pile as MainPage

  • Hello @Diegorafaelsouza, how do you suggest I make the changes? Have an example?

  • 1

    Dear @Diegorafaelsouza, you’re right! I changed my code to try to show you what I did. If you can give me the ways to find out where I’m going wrong, I’d appreciate it.

  • Now I can’t answer, but soon I’ll take a look if I can help you.

  • Dear @Diegorafaelsouza, you’ve had a while to see where my mistake is?

  • Yes. The problem is that you are not browsing. You are replacing the content of a single page. No event ClickTap2 instead of replacing Happy, do Navigarion.PushAsync(page); The way you’re doing it on page3. The way you’re copying it isn’t gonna work anyway. Then put as an answer with a full example

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.