xamarin - 如何基于其中包含 StackLayout 的 Frame 创建新的 Xamarin 元素?

标签 xamarin xamarin.forms

我有一个里面有 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/

相关文章:

xamarin.ios - Xamarin 表单选择器 - 自定义 UIPickerView

c# - iOS 不支持全局 PushAsync,请使用 NavigationPage

c# - 无法从内容页面执行 PopAsync、PopModalAsync 和 PopToRootAsync

c# - 如何在 android google map v2 上放置标记/绘图经度和纬度

xamarin.forms - 为什么当我尝试打开 Xamarin.Mac 项目 "MonoDevelop.MonoMac.XamMac2ProjectFlavor"时出现此错误?

xamarin.forms - Xamarin Forms ScrollView 上的 ScrollOrientation.Both

c# - 如何创建一个在后台运行的计时器而不用 Xamarin 阻塞 UI 线程?

android - Xamarin 无法使用 : "Unexpected install output: cmd: Can' t find service: package"将应用程序部署到模拟器

c# - 自定义标签中的 Xamarin 可点击子字符串

android - Xamarin.Android EditText 事件未在设备上触发