Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在Xamarin表单中为Xaml设置一个';这是重复的吗?_Xamarin_Xamarin.forms - Fatal编程技术网

我可以在Xamarin表单中为Xaml设置一个';这是重复的吗?

我可以在Xamarin表单中为Xaml设置一个';这是重复的吗?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我在我的应用程序中多次使用相同的XAML,但有两个细微差异,即我传入的文本和所选内容的值: <ViewCell Tapped="selectValue" > <Grid VerticalOptions="CenterAndExpand" Padding="20,0" > <local:StyledLabel Text="{Binding [1].Name}" HorizontalOptions="StartAndExpand" />

我在我的应用程序中多次使用相同的XAML,但有两个细微差异,即我传入的文本和所选内容的值:

<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>
我不确定这是否是100%的方法,但当我尝试实现这一点时,我得到的信息是:

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

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

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


为了让您开始,您可能想看看我的一篇博客文章:

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的数据模板:

<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>

第页:


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

然后在此页面中,只需添加如下内容:

<customView:MyCustomView />


谢谢杰拉尔德,你知道我可以从哪一个例子开始复制吗?更新了答案谢谢米兰,但这与我的需要有点不同,因为我需要传递几个值,还需要处理抽头事件。我已经更新了我的问题与我到目前为止。也许你可以看看这个,看看你是否能想到如何处理这个问题。ThanksSeems-like没有在其类中自动生成事件,因为您可能已经复制粘贴了此行。从xaml中删除此事件和名称,然后重新写入或重新生成它。如果不是自动生成的,那么您必须手动在其cals中写入此缺少的事件方法。谢谢,是的,我复制并粘贴到XAML中了。你能告诉我如何使它自动生成,或者如何将缺少的方法写入类中吗?我会接受你的答案,因为我认为这是两个方法中最具描述性的。我还有一个密切相关的问题,你很快就会明白。谢谢你知道这有帮助。您可以在这个类中手动添加Tapped事件作为void selectedValue(objectsender,System.EventArgs e){//您的代码在这里}
<ListView ItemSource="{Binding PersonList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <local:PersonCell />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<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 />