如何基于一个包含StackLayout的Frame创建一个新的Xamarin元素?

4
我有一个Frame,里面有一个StackLayout:
<Frame CornerRadius="1" HasShadow="false" Margin="10" 
 BackgroundColor="White" BorderColor="Silver" Padding="0" >
   <StackLayout Orientation="Vertical" Spacing="0" Padding="0" >
      <xaml:PtiXaml />
      <template:LineTemplate />
      <xaml:AtiXaml />
      <template:LineTemplate />
      <xaml:StiXaml />
   </StackLayout>
</Frame>

我可以创建一个名为NewFrame的新对象,它与包含StackLayout的Frame相同吗?

<template:NewFrame>
   <xaml:PtiXaml />
   <template:LineTemplate />
   <xaml:AtiXaml />
   <template:LineTemplate />
   <xaml:StiXaml />
</template:NewFrame>

或者
<template:NewFrame>
   <xaml:ABCXaml />
</template:NewFrame>

或者
<template:NewFrame>
   <Label Text="X" />
</template:NewFrame>

建议我使用自定义视图,但我查找了一下,没有找到其中包含其他元素的示例。


你看过ContentProperty属性了吗? - Tom
2个回答

0
在您的解决方案资源管理器中,右键单击共享项目(或PCL)中所需位置(我建议添加名为“Views”或“CustomViews”的文件夹并在其中创建项),选择“添加新项”,然后选择“Content View”(不带(C#))。文件名应该是类似于“View1.xaml”的东西,您可以根据自己的喜好更改它,但重要的是xaml扩展名存在。
这将创建一个新的ContentView,其中包含xaml和xaml.cs文件。在xaml文件中,您可以声明上面发布的xaml代码,并将任何必要的代码编写到xaml.cs文件中。
现在,您可以向要放置视图的页面添加命名空间声明:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        ...
        xmlns:customs="clr-namespace:YourNamespace.Views;assembly=YourNamespace"

并在该页面或任何布局的内容中声明该元素:

<customs:CustomViewName ... />

如果您想要控制元素的行为,可以在代码后台添加BindableProperties。
如需更深入的信息,您可能需要查看这篇文章:https://visualstudiomagazine.com/articles/2017/10/01/add-custom-controls.aspx

我的问题是它只是一个框架,框架内有不同的XAML代码。不仅仅是一组相同的XAML。你知道我该怎么处理吗? - Alan2
不,我没有看到任何好的方法来做到这一点。你可以创建多个类,每个类对应一个变体。 - Markus Michel

0
使用 ContentViewControlTemplate 创建自定义控件。这样,您可以创建一个名为NewFrame的新控件,编写您的控件的 XAML,然后在您的<ControlTemplate>中使用<ContentPresenter>标签指定您希望内容出现的位置。

就像这样:

.
└── NewFrame
    ├── NewFrame.cs
    └── NewFrame.xaml  -> Is a ResourceDictionary

NewFrame.cs:

namespace TestApp.Controls
{
    public partial class NewFrame : ContentView
    {
    }
}

NewFrame.xaml:

<ResourceDictionary 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:newFrame="clr-namespace:TestApp.Controls"
    x:Class="Namespace.For.A.ResourceDictionary">


    <Style TargetType="newFrame:NewFrame">
        <Setter Property="ControlTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <ContentView BackgroundColor="Transparent">
                        <Frame CornerRadius="1" HasShadow="false" Margin="10" BackgroundColor="White" BorderColor="Silver" Padding="0" >
                            <StackLayout Orientation="Vertical" Spacing="0" Padding="0">
                                <ContentPresenter/>
                            </StackLayout>
                        </Frame>
                    </ContentView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

ConsumingYourControl.xaml:

 <template:NewFrame>
    <template:NewFrame.Content>
        <xaml:PtiXaml />
        <template:LineTemplate />
        <xaml:AtiXaml />
        <template:LineTemplate />
        <xaml:StiXaml />
    </template:NewFrame.Content>
</template:NewFrame>

<template:NewFrame>
    <template:NewFrame.Content>
        <xaml:ABCXaml />
    </template:NewFrame.Content>
</template:NewFrame>

<template:NewFrame>
    <template:NewFrame.Content>
        <Label Text=""/>
    </template:NewFrame.Content>
</template:NewFrame>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接