0

我需要创建一个可以在多个页面上使用的标准布局,但传入显示在里面的内容......我的成功有限......

这是我调用自定义控件的方式...

<?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:control="clr-namespace:myApp"
             x:Class="myApp.MainPage">
    <StackLayout>

        <control:CustomPopupLayoutControl 
             BackgroundColor="LightGreen">

            <control:CustomPopupLayoutControl.Content>

                <Button Text="Hello"  />
                <!-- lots of other controls, buttons, labels etc, layout -->
            </control:CustomPopupLayoutControl.Content>

        </control:CustomPopupLayoutControl>

    </StackLayout>
</ContentPage>

所以你可以在这里看到我不想显示一些内容..

这是我的自定义控件...

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myApp.CustomPopupLayoutControl"
             x:Name="CustomPopupLayouts">
    <ContentView.Content>
        <StackLayout BackgroundColor="LightBlue" Margin="30">

            <Button Text="Close" /><!-- important-->
            <!-- lots of other layout -->
            <StackLayout BackgroundColor="Red" x:Name="Inner">

            </StackLayout>
         </StackLayout>
    </ContentView.Content>
</ContentView>

所以我想在内部堆栈布局中显示我的 hello 按钮,我还需要绑定才能工作......

所以最后一页应该看起来像......

<page>

  <StackLayout BackgroundColor="LightBlue" Margin="30">
    <Button Text="Close" /><!-- important-->

    <StackLayout BackgroundColor="Red">
      <button text="Hello">
    </StackLayout>
  <StackLayout>

</page>
4

2 回答 2

0

由于您已经在 Custom Control 中定义了一个Button,您只需将要在Button上显示的Title从 ContentPage 传递给 Custom Control 。

在 CustomPopupLayoutControl.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myApp.CustomPopupLayoutControl"
             x:Name="CustomPopupLayouts">
    <ContentView.Content>
        <StackLayout BackgroundColor="LightBlue" HeightRequest="450" Margin="30">

            <Button HeightRequest="150" WidthRequest="80" Text="{Binding Source={x:Reference CustomPopupLayouts}, Path=ButtonTitle}" /><!-- important-->

            <StackLayout BackgroundColor="Red" HeightRequest="300" x:Name="Inner">

            </StackLayout>
         </StackLayout>
    </ContentView.Content>
</ContentView>

在 CustomPopupLayoutControl.xaml.cs

定义可绑定属性

public static BindableProperty ButtonTitleProperty =
            BindableProperty.Create(nameof(ButtonTitle), typeof(string), typeof(CustomPopupLayoutControl),string.Empty);

        public string ButtonTitle
        {
            get => (string)GetValue(ButtonTitleProperty);
            set => SetValue(ButtonTitleProperty, value);
        }

在内容页面

您可以直接设置按钮的标题或使用数据绑定。

<control:CustomPopupLayoutControl ButtonTitle = "Hello World!" BackgroundColor="LightGreen" /  >
于 2020-11-13T09:00:56.347 回答
0

我找到了解决方案:)

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myapp.CustomPopupLayoutControl"
             x:Name="CustomPopupLayouts">
    <ContentView.ControlTemplate>
        <ControlTemplate>
            <StackLayout BackgroundColor="LightBlue" Margin="30">

                <Frame CornerRadius="5" Margin="20" HasShadow="False" 
                       BackgroundColor="Red">
                    <StackLayout>
                        <Button Text="Close" 
                           Command="{TemplateBinding 
                           Parent.BindingContext.NavigateCommand}" />
                        <!-- important-->

                        <ContentPresenter />
                    </StackLayout>
                </Frame>
            </StackLayout>
        </ControlTemplate>
    </ContentView.ControlTemplate>
</ContentView>

.

<?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:control="clr-namespace:myapp"
             x:Class="myapp.MainPage">
    <StackLayout>

        <control:CustomPopupLayoutControl BackgroundColor="LightGreen">

            <StackLayout>
                <Button Text="Hello" Command="{Binding NavigateCommand}" />

                <Button Text="Goodbye"  />
            </StackLayout>
        </control:CustomPopupLayoutControl>

    </StackLayout>
</ContentPage>
于 2020-11-13T13:46:21.827 回答