【问题标题】:How to add a grid view under Master-Detail detail page in Xamarin Forms?如何在 Xamarin Forms 的 Master-Detail 详细信息页面下添加网格视图?
【发布时间】:2020-03-29 03:33:35
【问题描述】:

我正在尝试在屏幕底部添加一个网格视图,其中包含有关媒体播放器中正在播放的媒体的“当前播放”信息。问题是,无论页面是什么可见的,我都希望这个视图存在。

主布局是导航菜单面板的主从页面,其中详细页面应包含所有内容。

详细信息页面可以是内容页面、导航页面(大部分)或模式内容页面。但是,如果我只能选择一个,我会选择它作为导航页面。

所以,我只是想做一些这样的事情:

<MasterDetailPage.Detail>
        <ContentPage>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>

                <Page x:Name="viewPort" Grid.Row="0">
                    <!--This page source is dynamically set from the code behind...-->
                </Page>

                <Grid Grid.Row="2">
                    <!--Here should be the rest of my grid structure...-->
                </Grid>
            </Grid>
        </ContentPage>
</MasterDetailPage.Detail>

但是不可能将页面包装在另一个视图/页面中,我还尝试修改主从页面控件模板以在详细信息页面的底部添加该网格并显示其上方的任何页面,但没有找到运气原始模板,甚至为 Master-Detail 布局设置模板...

我是 Xamarin 的新手,但对 c# 和 xaml 有一定的经验,非常感谢任何帮助。

【问题讨论】:

    标签: c# forms xaml xamarin xamarin.forms


    【解决方案1】:

    您可以将ContentPresenter 与控制模板一起使用。

    在 App.xaml> Application.Resources> ResourceDictionary 中创建控件模板。

      <!--  Grid Template -->
            <ControlTemplate x:Key="GridTemplate">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <ContentPresenter Grid.Row="0" />
                    <Label
                        Grid.Row="1"
                        BackgroundColor="Accent"
                        Text="Grid Template" />
                </Grid>
            </ControlTemplate> 
    

    然后在详情页使用。

    ControlTemplate="{StaticResource GridTemplate}"
    

    我已将项目上传到 GitHub 供您参考。 https://github.com/WendyZang/Test/tree/master/ControlTemplate_ContentPresenter/MasterDetailPageDemo

    更新:

    控件模板的数据绑定可以使用TemplateBinding

    创建一个 AppViewModel:

     public class AppViewModel
    {
        public string Name { get; set; } = "Name_A";
    
        public AppViewModel()
        {
    
        }
    }
    

    App.xml:

       <ControlTemplate x:Key="GridTemplate">
    
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
    
                    <ContentPresenter Grid.Row="0"  />
                    <Label Grid.Row="1" BackgroundColor="Accent" Text="{TemplateBinding BindingContext.Name}" />
    
                </Grid>
            </ControlTemplate>
    

    在每个使用控件模板的页面中设置绑定。

      this.BindingContext = new AppViewModel();
    

    【讨论】:

    • 感谢您的帮助,但是,这就是我不想做的事情,因为现在网格在两个页面中确实可见,我可以对项目页面使用相同的原则好吧,但问题是每次页面更改时,都会重新创建整个网格,现在我不再有静态网格的感觉,它只是闪烁意味着它重新加载。此外,我不再拥有相同的控件,这意味着我将不得不处理未知数量的重新加载,最终我将创建全局变量来设置......或者可能是数据绑定......但同样,感谢您的帮助。
    • 我的回复更新了如何使用控件模板的绑定。可以参考一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2019-07-19
    • 2016-09-22
    • 2015-04-08
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多