【问题标题】:Can I set up a template in Xamarin Forms for Xaml that's repetitive?我可以在 Xamarin Forms for Xaml 中设置一个重复的模板吗?
【发布时间】:2018-07-15 18:38:27
【问题描述】:

我在我的应用程序中多次使用相同的 XAML,但有两个细微差别,即我传入的 Text 和 Selected 的值:

<ViewCell Tapped="selectValue" >
   <Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
      <local:StyledLabel Text="{Binding [1].Name}" HorizontalOptions="StartAndExpand" />
      <local:StyledLabel IsVisible="{Binding [1].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
   </Grid>
</ViewCell>

Xamarin 表单是否有任何模板功能,例如,我可以将其缩短为:

<local:SwitchViewCell Text="{Binding [1].Name}" Selected="{Binding [1].IsSelected}" />

这是我目前所拥有的:

<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          mlns:local="clr-namespace:Japanese;assembly=Japanese"
          x:Class="Japanese.SwitchViewCell""
          Tapped="selectValue" >
   <Grid VerticalOptions="CenterAndExpand" Padding="20,0" >
      <local:StyledLabel Text="{Binding Text, Source={x:Reference this}}" HorizontalOptions="StartAndExpand" />
      <local:StyledLabel IsVisible="{Binding IsVisible, Source={x:Reference this}}" TextColor="Gray" HorizontalOptions="End" Text="✓" />
   </Grid>
</ViewCell>

现在有了这段代码:

namespace Japanese.Templates
{
    public partial class SwitchViewCell : ViewCell
    {
        public SwitchViewCell()
        {
            InitializeComponent();
        }

        public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(SwitchViewCell));
        public static readonly BindableProperty IsVisibleProperty = BindableProperty.Create(nameof(IsVisible), typeof(bool), typeof(SwitchViewCell));

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public bool IsVisible
        {
            get
            {
                return (bool)GetValue(IsVisibleProperty);
            }
            set
            {
                SetValue(IsVisibleProperty, value);
            }
        }


    }
}

我不确定这是否是 100% 的路要走,但是当我尝试实施此操作时,我收到消息:

 EventHandler "selectValue" not found in type "Japanese.Templates.SwitchViewCell" (Japanese)

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    ListView 单元格:

    对于 ListView 单元格,您可以定义 ListView 项的 ViewCell 布局。 前任。 PersonCell.xaml

    <ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="DataTemplates.PersonCell">
         <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.3*" />
                <ColumnDefinition Width="0.3*" />
                <ColumnDefinition Width="0.4*" />
            </Grid.ColumnDefinitions>
            <Label Text="{Binding FirstName}" />
            <Label Grid.Column="1" Text="{Binding LastName}" />
            <Label Grid.Column="2" Text="{Binding Email}" />
        </Grid>
    </ViewCell>
    

    然后你可以在 ListView 的 DataTemplate 中使用它:

    <ListView ItemSource="{Binding PersonList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <local:PersonCell />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    这样,ListView 可以重新使用每个项目的单元格设计。

    可重用视图:

    您还可以创建一个可重复使用的视图,该视图可以简单地包含在您的页面中。 前任。 MyCustomView.xaml:

    <Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xam.Control.MyCustomView"> 
        <StackLayout>
            <Label Text="Hello"/>
            <Label Text="How Are You?"/>
        </StackLayout>
    </Grid>
    

    页面:

    <ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:customView="clr-namespace:Xam.Control;assembly=Xam"
    x:Class="Xam.View.HomePage">
    

    请注意,您必须包含自定义视图所在的命名空间和程序集。

    然后在这个页面中,简单的添加如下:

    <customView:MyCustomView />
    

    【讨论】:

    • 谢谢米兰,但这与我的需求有点不同,因为我需要传递几个值并处理点击事件。我已经用到目前为止的内容更新了我的问题。也许您可以看一下,看看您是否可以考虑例如如何处理窃听。谢谢
    • 似乎 没有在其类中自动生成事件,因为您可能已经复制粘贴了这一行。从 xaml 中删除此事件和名称并重新编写或重新生成它。如果不自动生成,则必须手动在其 calss 中编写此缺少的事件方法。
    • 谢谢,是的,我确实复制并粘贴到了 XAML 中。你能告诉我如何让它自动生成或如何将缺少的方法写入类吗?
    • 我会接受您的回答,因为我认为这是两者中最具描述性的。我有另一个密切相关的问题,所以你很快就会看到。谢谢
    • 很高兴知道它有帮助。好吧,您可以在此类中手动将 Tapped 事件添加为 void selectedValue (object sender, System.EventArgs e) { //your code goes here }
    【解决方案2】:

    当然,只需将其放在单独的 XAML 文件中并定义您需要的可绑定属性并将值映射到自定义控件中的控件。事实上,后者并非绝对需要,但更好的是使其完全可重用。

    如果您只想在项目中重复使用它并始终将其数据绑定到相同的字段,您可以保持原样,因为 BindingContext 将被继承。

    为了让你开始,你可能想看看我的一篇博客文章:https://blog.verslu.is/xamarin/xamarin-forms-xamarin/reusable-custom-usercontrols-with-bindableproperty/

    【讨论】:

    • 谢谢 Gerald,你知道我可以复制的任何例子吗?
    • 更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多