简体   繁体   English

从 ContentPage Xamarin.Forms Android 打开另一个 XAML

[英]Open another XAML from a ContentPage Xamarin.Forms Android

I have a MainPage.xaml that contains my home page.我有一个包含我的主页的 MainPage.xaml。 In the code below, I named the button as btnDrawer so that when a user clicks on it, another page pops up from the side, exactly like a side bar.在下面的代码中,我将按钮命名为 btnDrawer,这样当用户点击它时,另一个页面会从侧面弹出,就像一个侧边栏。

<!-- Contains header/action bar -->
<StackLayout Padding="20" Grid.Row="0" Orientation="Horizontal">

    <Image Source="icon_drawer.png" WidthRequest="30" x:Name="btnDrawer"/>
    <Label Text="Dylan Villaruel" TextColor="White" FontSize="Medium" VerticalOptions="Center" Margin="5, 0, 0, 0"/>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="9*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Image Source="icon_dropdown.png" Grid.Column="1" VerticalOptions="Center" WidthRequest="20"/>
    </Grid>
</StackLayout>

This is my MainPage class:这是我的 MainPage 类:

public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Drawer_Click();
        }

        public void Drawer_Click()
        {
            btnDrawer.GestureRecognizers.Add(new TapGestureRecognizer() { 
                Command = new Command(() =>
                {
                    DisplayAlert("Task", "You clicked me", "Okay");
                })     
            });
        }
    }

I've just put in DisplayAlert as a placeholder.我刚刚将 DisplayAlert 作为占位符放入。 I tried to put Navigation.PushAsync(new Drawer()) ;我试着把Navigation.PushAsync(new Drawer()) ; but it doesn't work.但它不起作用。 I'm trying to call an XAML/Class named Drawer.我正在尝试调用名为 Drawer 的 XAML/类。 I don't want to use a Master-Detail Page because I'm new to C# and I want to learn how to do things manually, it makes me understand how things work.我不想使用主从页面,因为我是 C# 新手,我想学习如何手动做事,这让我了解事情是如何工作的。

Instead of using button as below, you can also have tap gesture on your image and place the event in code behind(xaml.cs) page除了使用下面的按钮,您还可以在图像上点击手势并将事件放置在代码隐藏(xaml.cs)页面中

Your xaml page:您的 xaml 页面:

<Button Text="Main Page" BackgroundColor="Blue" Clicked="NavigateButton_OnClicked">  
</Button> 

your code beind:你的代码隐藏在:

private async void NavigateButton_OnClicked(object sender, EventArgs e) {  
            await Navigation.PushAsync(new SecondPage());  
        }  

Please find description here: Navigation between pages请在此处找到说明: 页面之间的导航

Did you get the following error when you click the Image to execute Navigation.PushAsync(new Drawer()) ?您在单击Image执行Navigation.PushAsync(new Drawer())时是否收到以下错误?

System.InvalidOperationException: 'PushAsync is not supported globally on Android, please use a NavigationPage.'

If so, you can use Navigation.PushModalAsync(new Drawer());如果是这样,您可以使用Navigation.PushModalAsync(new Drawer()); to replace it.更换它。 Here is running GIF.这里正在运行 GIF。

在此处输入图片说明

If you still want to use the Navigation.PushAsync(new Drawer()) , you can use MainPage =new NavigationPage( new MainPage());如果你仍然想使用Navigation.PushAsync(new Drawer()) ,你可以使用MainPage =new NavigationPage( new MainPage()); in the App.xaml.cs like this code.App.xaml.cs像这样的代码。

    public App()
    {
        InitializeComponent();

        MainPage =new NavigationPage( new MainPage());
    }

If you want to click the Image , you can also use ImageButton to achieve it .如果你想点击的Image ,你也可以使用ImageButton来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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