xamarin.forms - 我可以创建一些小视图,包括创建的视图以在 mainPage 中使用它吗?
问题描述
我可以创建一个包含条目进度条或其他的视图来制作一个可以在其他地方使用的视图吗?
例如,我想创建一个可用于列出列表的卡片视图。
我只能写在列表中,而不是在每个列表中都写卡片。
解决方案
是的,您可以创建自定义视图并在您想要的MainPage
或其他Pages
中使用它。Views
要创建自定义视图,请添加new item
--> ContentView
,我们称之为CardView
:
在 .cs 中CardView
:
public partial class CardView : ContentView
{
public CardView()
{
InitializeComponent();
}
}
在 CardView 的 xaml 中,添加您的labels
, progressbar
,entry
有:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="App35.CardView">
<ContentView.Content>
<StackLayout>
<Label Text="Hello Xamarin.Forms!" />
<ProgressBar Progress="0.5" />
<Entry Placeholder="I'm entry"/>
</StackLayout>
</ContentView.Content>
</ContentView>
在MainPage
、 在listView
或 在 中使用它page
:
<StackLayout>
<projectName:CardView HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
<ListView x:Name="listView" RowHeight="70">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>mono</x:String>
<x:String>monodroid</x:String>
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<projectName:CardView HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
我在这里上传了一个样本,你可以检查一下。
推荐阅读
- php - 如何“检查 POST Array 上是否有重复值”?
- java - 用本地克隆替换 gradle 依赖
- html - 设备兼容 CSS - @media
- project-reactor - 如何以反应器方式编写快捷循环
- oracle-cloud-infrastructure - 引导卷和块卷有什么区别?
- json - 有没有办法利用 JSON 路径来填写搜索字段并以超链接的形式返回搜索结果?
- c# - 如何获取MEF中的所有方法
- react-native - COMPONENT 已从 react-native 核心中提取,并将在未来的版本中删除。现在可以从新位置安装和导入它
- c++ - 在 C++ 中获取多行句子输入
- c - 启用配置文件引导优化时,OpenMP 并行元素增量得到错误结果