首页 > 解决方案 > 我可以创建一些小视图,包括创建的视图以在 mainPage 中使用它吗?

问题描述

我可以创建一个包含条目进度条或其他的视图来制作一个可以在其他地方使用的视图吗?

例如,我想创建一个可用于列出列表的卡片视图。

我只能写在列表中,而不是在每个列表中都写卡片。

标签: xamarin.forms

解决方案


是的,您可以创建自定义视图并在您想要的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>

我在这里上传了一个样本,你可以检查一下。


推荐阅读