Xaml TabbedPage中的Xamarin表单添加按钮

Xaml TabbedPage中的Xamarin表单添加按钮,xaml,xamarin,xamarin.forms,xamarin.android,xamarin.ios,Xaml,Xamarin,Xamarin.forms,Xamarin.android,Xamarin.ios,我有个问题。我创建了以下选项卡页: <?xml version="1.0" encoding="utf-8" ?> <TabbedPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:

我有个问题。我创建了以下选项卡页:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:d="http://xamarin.com/schemas/2014/forms/design"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:views="clr-namespace:MyApp.Views"
            mc:Ignorable="d"
            x:Class="MyApp.Views.MainPage"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            android:TabbedPage.ToolbarPlacement="Bottom"
            BarBackgroundColor="White"
            BarTextColor="Black"
            android:TabbedPage.BarItemColor="#B2B2B2"
            android:TabbedPage.BarSelectedItemColor="#56D7A5"
            android:TabbedPage.IsSwipePagingEnabled="False">    

    <TabbedPage.Children>
        <NavigationPage Title="page1" IconImageSource="navbar_page1">
            <x:Arguments>
                <views:page1 NavigationPage.HasNavigationBar="False" />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="page2" IconImageSource="navbar_page2">
            <x:Arguments>
                <views:page2 NavigationPage.HasNavigationBar="False" />
            </x:Arguments>
        </NavigationPage>

        <NavigationPage Title="page3" IconImageSource="navbar_page3">
            <x:Arguments>
                <views:page3 NavigationPage.HasNavigationBar="False" />
            </x:Arguments>
        </NavigationPage>
    
</TabbedPage>

现在,我在每一页上都添加了如下自定义菜单:

<c:FloatingMenu Margin="0, 0, 10, 10" BGColor="#56D7A5" OpenIcon="openFab_icon" CloseIcon="closeFab_icon"
                AbsoluteLayout.LayoutBounds=".95,.95" AbsoluteLayout.LayoutFlags="PositionProportional">
    <c:FloatingButton x:Name="btnAddHomework" BGColor="#59E1FF" IconSrc="add_homework_icon" OnClickCommand="{Binding btnAddHomeworkCommand}" />
    <c:FloatingButton x:Name="btnAddDeadline" BGColor="#0FF1A0" IconSrc="add_deadline_icon"/>
    <c:FloatingButton x:Name="btnAddTest" BGColor="#5988FF" IconSrc="add_test_icon"/>
</c:FloatingMenu>

问题是每个页面都有自己的菜单,所以你会看到它消失并重新出现在每个页面上,所以我的问题是:是否有某种根视图覆盖了TabbedPage中的所有选项卡


请让我知道我是怎么做到的

作为一种解决方法,您可以设置每个内容页的(或者您可以定义一个基本内容页)


我建议创建一个包含
静态浮动按钮的
BaseContentPage
。这允许每个页面继承自
BaseContentPage
,并使用相同的
FloatingButton

代码 BaseContentPage

抽象类BaseContentPage:ContentPage
{
受保护的静态按钮按钮{get;}=new按钮{Text=$“此按钮是在{DateTimeOffset.UtcNow}}创建的。调用(按钮=>Button.Clicked+=HandleButtonClicked);
静态异步void HandleButtonClicked(对象发送方,事件参数e)=>
等待Application.Current.MainPage.DisplayAlert(“单击按钮”,“这是两个页面上的相同按钮”,“确定”);
}
示例标签页

类标签页:基本按钮页
{
公共标签页()
{
Title=“LabelPage”;
内容=新的堆栈布局
{
孩子们=
{
新标签{Text=“Label Page”}.TextCenter().Center(),
按钮
}
}
}
}
示例按钮页面

类按钮页面:基本按钮页面
{
公共按钮页()
{
Title=“ButtonPage”;
内容=按钮;
}
}
示例应用程序

公共类应用程序:应用程序
{
公共应用程序()
{
SetFlags(新[]{“标记”});
主页=新选项卡页
{
孩子们=
{
新建按钮页面(),
新标签页()
}
};
}
}
示例应用程序 以下是用于创建附加GIF的示例应用程序:

免责声明 我想出了一种只使用纯Xamarin.Forms创建想要的效果的方法。阅读并注意解决方案中的棘手部分

摘要 此解决方案是通过实现
绝对布局
旋转视图
指示视图
数据模板选择器
实现的Xamarin.Forms 4.8假设如下。如果使用较低版本,请考虑到
CarouselView
IndicatorView
等功能可能处于预览状态

DataTemplateSelector
CarouselView
IndicatorView
用于模拟
选项卡页面
,而
绝对布局
用于提供覆盖

因此,现在有了解决方案:

创建您的视图 在这里,您可以为所需的每个页面创建一个视图。在本例中,我希望我的应用程序由两个页面组成,因此我创建了两个视图(代码隐藏保持不变):

View1.xaml

<?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:t="clr-namespace:overlayTest"
             x:Class="overlayTest.Page1">
    <ContentPage.Resources>
        <ResourceDictionary>
            <t:MyTemplateSelector x:Key="templateSelector"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <AbsoluteLayout>
            <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     Padding="0"
                     Spacing="0">
                <CarouselView ItemTemplate="{StaticResource templateSelector}"
                          IndicatorView="indicatorView">
                    <CarouselView.ItemsSource>
                        <x:Array Type="{x:Type x:String}">
                            <x:String>1</x:String>
                            <x:String>2</x:String>
                        </x:Array>
                    </CarouselView.ItemsSource>
                </CarouselView>

                <IndicatorView x:Name="indicatorView">
                    <IndicatorView.IndicatorTemplate>
                        <DataTemplate>
                            <StackLayout HorizontalOptions="FillAndExpand">
                                <Frame Margin="10">
                                    <Label/>
                                </Frame>
                            </StackLayout>
                        </DataTemplate>
                    </IndicatorView.IndicatorTemplate>
                </IndicatorView>

            </StackLayout>

            <ContentView 
                     IsVisible="True" VerticalOptions="Start"
                     AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     BackgroundColor="Transparent">
                <Frame CornerRadius="10"
                   Margin="20"
                   VerticalOptions="StartAndExpand"
                   HorizontalOptions="CenterAndExpand" InputTransparent="False">
                    <StackLayout Padding="0">
                        <Label 
                           FontSize="Medium"
                           TextColor="Black"/>

                        <StackLayout Orientation="Horizontal"
                                 HorizontalOptions="CenterAndExpand">
                            <Label Text="I am floating here"/>
                            <Switch IsToggled="True" />
                        </StackLayout>


                        <Button Text="Save"
                               BackgroundColor="Accent"/>
                    </StackLayout>
                </Frame>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

View2.xaml

<?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:t="clr-namespace:overlayTest"
             x:Class="overlayTest.Page1">
    <ContentPage.Resources>
        <ResourceDictionary>
            <t:MyTemplateSelector x:Key="templateSelector"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <AbsoluteLayout>
            <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     Padding="0"
                     Spacing="0">
                <CarouselView ItemTemplate="{StaticResource templateSelector}"
                          IndicatorView="indicatorView">
                    <CarouselView.ItemsSource>
                        <x:Array Type="{x:Type x:String}">
                            <x:String>1</x:String>
                            <x:String>2</x:String>
                        </x:Array>
                    </CarouselView.ItemsSource>
                </CarouselView>

                <IndicatorView x:Name="indicatorView">
                    <IndicatorView.IndicatorTemplate>
                        <DataTemplate>
                            <StackLayout HorizontalOptions="FillAndExpand">
                                <Frame Margin="10">
                                    <Label/>
                                </Frame>
                            </StackLayout>
                        </DataTemplate>
                    </IndicatorView.IndicatorTemplate>
                </IndicatorView>

            </StackLayout>

            <ContentView 
                     IsVisible="True" VerticalOptions="Start"
                     AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     BackgroundColor="Transparent">
                <Frame CornerRadius="10"
                   Margin="20"
                   VerticalOptions="StartAndExpand"
                   HorizontalOptions="CenterAndExpand" InputTransparent="False">
                    <StackLayout Padding="0">
                        <Label 
                           FontSize="Medium"
                           TextColor="Black"/>

                        <StackLayout Orientation="Horizontal"
                                 HorizontalOptions="CenterAndExpand">
                            <Label Text="I am floating here"/>
                            <Switch IsToggled="True" />
                        </StackLayout>


                        <Button Text="Save"
                               BackgroundColor="Accent"/>
                    </StackLayout>
                </Frame>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>

创建一个
DataTemplateSelector
这将由
旋转视图使用,以便根据当前
位置选择一个视图或另一个视图

使用系统;
使用Xamarin.Forms;
命名空间覆盖测试
{
类MyTemplateSelector:DataTemplateSelector
{
只读数据模板视图1、视图2;
公共MyTemplateSelector()
{
view1=新数据模板(typeof(view1));
view2=新数据模板(typeof(view2));
}
受保护的覆盖数据模板OnSelectTemplate(对象项,BindableObject容器)
{
字符串s=item.ToString();
如果(s==“1”)
{
返回视图1;
}
返回视图2;
}
}
}
创建你的主页 Page1.xaml

<?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:t="clr-namespace:overlayTest"
             x:Class="overlayTest.Page1">
    <ContentPage.Resources>
        <ResourceDictionary>
            <t:MyTemplateSelector x:Key="templateSelector"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <AbsoluteLayout>
            <StackLayout AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     Padding="0"
                     Spacing="0">
                <CarouselView ItemTemplate="{StaticResource templateSelector}"
                          IndicatorView="indicatorView">
                    <CarouselView.ItemsSource>
                        <x:Array Type="{x:Type x:String}">
                            <x:String>1</x:String>
                            <x:String>2</x:String>
                        </x:Array>
                    </CarouselView.ItemsSource>
                </CarouselView>

                <IndicatorView x:Name="indicatorView">
                    <IndicatorView.IndicatorTemplate>
                        <DataTemplate>
                            <StackLayout HorizontalOptions="FillAndExpand">
                                <Frame Margin="10">
                                    <Label/>
                                </Frame>
                            </StackLayout>
                        </DataTemplate>
                    </IndicatorView.IndicatorTemplate>
                </IndicatorView>

            </StackLayout>

            <ContentView 
                     IsVisible="True" VerticalOptions="Start"
                     AbsoluteLayout.LayoutBounds="0,0,1,1"
                     AbsoluteLayout.LayoutFlags="All"
                     BackgroundColor="Transparent">
                <Frame CornerRadius="10"
                   Margin="20"
                   VerticalOptions="StartAndExpand"
                   HorizontalOptions="CenterAndExpand" InputTransparent="False">
                    <StackLayout Padding="0">
                        <Label 
                           FontSize="Medium"
                           TextColor="Black"/>

                        <StackLayout Orientation="Horizontal"
                                 HorizontalOptions="CenterAndExpand">
                            <Label Text="I am floating here"/>
                            <Switch IsToggled="True" />
                        </StackLayout>


                        <Button Text="Save"
                               BackgroundColor="Accent"/>
                    </StackLayout>
                </Frame>
            </ContentView>
        </AbsoluteLayout>
    </ContentPage.Content>
</ContentPage>
最后将该页面设置为应用程序的MainPage属性:
public应用程序()
{
初始化组件();
主页=新页面1();
}
最终结果如下所示:

我只想说,这些回复中的大多数都不是真的。使用自定义渲染器向选项卡式页面添加按钮是可能的(尽管您可以说这并不容易)。参考这个问题:我的意思是一个按钮,当你切换标签时,它不会消失,所以标签会覆盖。这个链接是针对gabbar中的一个图像的,但这不是我的意思,我认为在你的情况下,ToolBarItem是一个很好的解决方法,这是最有可能的。我建议使用浮动菜单的静态实例@LucasZhang MSFT-作为微软的一名同事,我强烈建议你避免说“不可能”的事情,除非你百分之百肯定地知道这是不可能的。更好的回答是“我不知道如何实现这一点”,或者根本没有回答。我们都是微软的代表,我们给其他开发者的建议很有分量,会被当作事实来接受。标签页只是一个内容页面的容器,你需要在每个内容页面上添加浮动按钮。它不会浮动在标签上方,不。按钮显示在每个内容页面的内部。Brandon,任何事情都有可能。我也在寻找一种显示悬停在选项卡页面上方的视图的方法。我需要它显示全屏材料Dra