单击主页详细信息页面中

时间:2018-01-06 03:58:49

标签: ios xamarin xamarin.forms

我在iOS应用程序的xamarin.forms中使用主详细信息页面,我在左上角放置了一个图像,现在我想在点击该图像时导航到母版页,如何让它导航

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:MasterApp.Controls;assembly=MasterApp"
             x:Class="MasterApp.Views.Dashboard.MasterDetailPage">
    <MasterDetailPage.Master>
        <ContentPage Padding="10" Title="Master" >

        </ContentPage>
    </MasterDetailPage.Master>

    <MasterDetailPage.Detail>
        <ContentPage Icon="Menu Icon.png">
            <ContentPage.Content>

            </ContentPage.Content>
        </ContentPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>

在这里,我需要在点击详细信息页面中的图标时进入母版页。

3 个答案:

答案 0 :(得分:0)

您需要在拥有主布局的XAML中设置图标。只有在这里你设置图标。请查看official xamarin tutorial

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="MasterDetailPageNavigation.MasterPage"
         Padding="0,40,0,0"
         Icon="hamburger.png" // here you set icon for navigation 
         Title="Personal Organiser">
<StackLayout>
    <ListView x:Name="listView" SeparatorVisibility="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="5,10">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30"/>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Image Source="{Binding IconSource}" />
                        <Label Grid.Column="1" Text="{Binding Title}" VerticalOptions="FillAndExpand" />
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

=== EDIT === 尝试将Icon移至MasterDetailPage,也许这会影响图片图标的名称。因为如果你的名字有空格,那可能会有问题。

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:MasterApp.Controls;assembly=MasterApp"
         x:Class="MasterApp.Views.Dashboard.MasterDetailPage"
         Icon="Menu Icon.png">
<MasterDetailPage.Master>
    <ContentPage Padding="10" Title="Master" >

    </ContentPage>
</MasterDetailPage.Master>

<MasterDetailPage.Detail>
    <ContentPage Icon="Menu Icon.png">
        <ContentPage.Content>

        </ContentPage.Content>
    </ContentPage>
</MasterDetailPage.Detail>

答案 1 :(得分:0)

我做了一些解决方法,最后让它导航到掌握。

 <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:controls="clr-namespace:MasterApp.Controls;assembly=MasterApp"
                 x:Class="MasterApp.Views.Dashboard.MasterDetailPage">
        <MasterDetailPage.Master>
            <ContentPage Padding="10" Title="Master" >

            </ContentPage>
        </MasterDetailPage.Master>

        <MasterDetailPage.Detail>
            <ContentPage>
                <ContentPage.Content>
     <Image Source="Menu.png"  HorizontalOptions="StartAndExpand" VerticalOptions="Start">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_OnTapped"></TapGestureRecognizer>
                        </Image.GestureRecognizers>                      
                    </Image>
                </ContentPage.Content>
            </ContentPage>
        </MasterDetailPage.Detail>
    </MasterDetailPage>

在cs中,我写了

private async void TapGestureRecognizer_OnTapped(object sender, EventArgs e)
        {            
            IsPresented = true;
        }

答案 2 :(得分:0)

如果要显示图标并点按它以导航到母版页。我们应该嵌入一个NavigationPage来包装你的ContentPage,如:

<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <ContentPage>
                <ContentPage.Content>

                </ContentPage.Content>
            </ContentPage>
        </x:Arguments>
    </NavigationPage>
</MasterDetailPage.Detail>

实际上我们可以在母版页中显示图标,这是我提到的完整代码:

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MasterDetailDemo.MyMasterDetailPage"
             xmlns:pages="clr-namespace:MasterDetailDemo">
    <MasterDetailPage.Master>
        <ContentPage Padding="10" Title="Master" Icon="Menu Icon.png">

        </ContentPage>
    </MasterDetailPage.Master>
    <MasterDetailPage.Detail>
        <NavigationPage>
            <x:Arguments>
                <ContentPage>
                    <ContentPage.Content>
                       ...//add your control
                    </ContentPage.Content>
                </ContentPage>
            </x:Arguments>
        </NavigationPage>
    </MasterDetailPage.Detail>
</MasterDetailPage>