【问题标题】:ListView Alternate Row Color in XamarinXamarin 中的 ListView 备用行颜色
【发布时间】:2021-01-13 09:37:21
【问题描述】:

在我的 Xamarin 应用程序中,我使用 foreach 循环来获取可用凭据的 namevalue,然后使用 ListView 在屏幕上打印。

例如

名称 1
值 1

名称 2
价值 2

它工作正常。我现在想要的是改变 Name (_attributes.Add(item.Name.ToString());) 的样式。

例如

姓名 1
价值

名字 2
价值

View.xml

<ListView
    SeparatorVisibility="None"
    BackgroundColor="#FFFFFF"
    ItemsSource="{Binding Attributes}"
    HasUnevenRows="true">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label
                    Text="{Binding .}" 
                    TextColor="#000000"
                    FontSize="18"
                    Padding="10" >
                </Label>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ViewModel.cs

foreach (var item in _credential.CredentialAttributesValues)
{
    _attributes.Add(item.Name.ToString());
    _attributes.Add(item.Value.ToString());
}

private ObservableCollection<string> _attributes = new ObservableCollection<string>();
public ObservableCollection<string> Attributes
{
    get
    {
        return _attributes;
    }
    set
    {
        this.RaiseAndSetIfChanged(ref _attributes, value);
    }
}

更新

<ViewCell>
    <StackLayout>
        <Label
        Text="{Binding Name}" 
        TextColor="#000000"
        FontSize="18"
        Padding="10"
        Margin="10,0,0,0">
        </Label>

        <Label
        Text="{Binding Value}" 
        TextColor="#000000"
        FontSize="18"
        Padding="10"
        Margin="10,0,0,0">
        </Label>
    </StackLayout>
</ViewCell>
private ObservableCollection<CredentialPreviewAttribute> _attributes = new ObservableCollection<CredentialPreviewAttribute>();
public ObservableCollection<CredentialPreviewAttribute> Attributes
{
    get
    {
        return _attributes;
    }
    set
    {
        this.RaiseAndSetIfChanged(ref _attributes, value);
    }
}

【问题讨论】:

    标签: c# .net xamarin xamarin.forms


    【解决方案1】:

    由于您使用了字符串数据类型,因此很难理解集合中的哪个字符串将是名称,但是如果您想根据索引给出样式,您可以点击以下链接:

    https://blog.verslu.is/stackoverflow-answers/alternate-row-color-listview/

    假设您有名为 Credential 的模型

    public class Credential
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
    

    在你的 ViewModel 中

    private ObservableCollection<Credential> _attributes = new ObservableCollection<Credential>();
    public ObservableCollection<Credential> Attributes
    {
       get
       {
           return _attributes;
       }    
       set
       {
           this.RaiseAndSetIfChanged(ref _attributes, value);
       }
    }
    

    在 .xaml 中

    <ListView
        SeparatorVisibility="None"
        BackgroundColor="#FFFFFF"
        ItemsSource="{Binding Attributes}"
        HasUnevenRows="true">
    
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label
                            Text="{Binding Name}" 
                            TextColor="#000000"
                            Style="[Whatever you want...]"
                            FontSize="18"
                            Padding="10" >
                        </Label>
                        <Label
                            Text="{Binding Value}" 
                            TextColor="#000000"
                            FontSize="18"
                            Padding="10" >
                        </Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    您还可以使用单个标签来实现问题中请求的视图要求。

    <Label>
       <Label.FormattedText>
          <FormattedString>
             <Span Text="{Binding Name}" TextColor="Black" />
             <Span Text="&#10;" />
             <Span Text="{Binding Value}" TextColor="Gray" />
          </FormattedString>
       </Label.FormattedText>
    </Label>
    

    【讨论】:

    • 这是名字_attributes.Add(item.Name.ToString()); ... ???
    • 无法打开此链接。你可以吗?
    • 是的,任何人都可以看到它,但是,在您的 ViewModel 中,您已经创建了字符串数据类型的集合。我建议您在 ViewModel 中创建对象(项目类型)的集合,然后很容易识别哪个是您的名字,哪个是值。
    • 在您的视图模型中,您仍然有 observableCollection 字符串,使其成为 credentialpreviewattribute 类的集合,并在集合中添加整个项目而不是 item.name 或 item.value 并更新您的视图,如我在我的例如你会得到它。
    • _attributes.Add(item) 像这样添加项目。
    【解决方案2】:

    View.xml

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label
                    Text="{Binding .}" 
                    TextColor="#000000"
                    Style="{StaticResource LabelStyle}"
                    FontSize="18"
                    Padding="10" >
                </Label>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
    

    Style.xaml

    <ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NameProject.Style.GeneralStyle">
    
    <Style TargetType="Label" x:Key="LabelStyle1">
        <Setter Property="Padding">
            <Setter.Value>
                <OnIdiom x:TypeArguments="Thickness" Phone="10" Tablet="50"/>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize">
            <Setter.Value>
                <OnIdiom x:TypeArguments="x:Double" Phone="18" Tablet="24"/>
            </Setter.Value>
        </Setter>
        <Setter Property="TextColor" Value="red"/>
    </Style>
    
    <Style TargetType="Label" x:Key="LabelStyle2">
        <Setter Property="Padding">
            <Setter.Value>
                <OnIdiom x:TypeArguments="Thickness" Phone="12" Tablet="50"/>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize">
            <Setter.Value>
                <OnIdiom x:TypeArguments="x:Double" Phone="20" Tablet="24"/>
            </Setter.Value>
        </Setter>
        <Setter Property="TextColor" Value="Blue"/>
    </Style>
    
    </ResourceDictionary>
    

    Style.cs

    public static General SharedInstance { get; } = new General();
    

    App.cs

    dictionary.MergedDictionaries.Add(Style.General.SharedInstance);
        
        
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
    猜你喜欢
    • 2016-08-18
    • 2021-12-05
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多