我有一个里面有 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>
有人建议我使用自定义 View ,但我已经查看并找不到其中包含其他元素的示例。
最佳答案
右键单击解决方案资源管理器中共享项目(或 PCL)中的所需位置(我建议添加一个名为“Views”或“CustomViews”的文件夹并在该文件夹中创建项目),选择“添加新项目”,然后选择“内容 View ”(后面没有 (C#)。文件名应该类似于“View1.xaml”,您可以根据自己的喜好更改它,但重要的是 xaml 扩展名在那里。
这将使用 xaml 和 xaml.cs 文件创建一个新的 ContentView。
在 xaml 文件中,您可以声明上面发布的 xaml 代码,并将任何必要的代码写入 xaml.cs 文件中。
现在,您可以向要将 View 放入的页面添加命名空间声明:
<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
关于xamarin - 如何基于其中包含 StackLayout 的 Frame 创建新的 Xamarin 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53422182/