简体   繁体   English

如何在StackLayout中更改颜色?

[英]How to change color in StackLayout?

I developing app with Visual Studio to Android , and I have file ItemsPage.xaml with this code : 我使用Visual Studio将应用程序开发到Android ,并且我的文件ItemsPage.xaml具有以下代码:

<ContentPage.Content>
    <StackLayout>
        <ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout x:Name="General" Orientation="Horizontal" HorizontalOptions="Fill" Padding="5">
                            <Image Source="{Binding FileName, Converter={StaticResource ImageConverter}}" HeightRequest="150" WidthRequest="150" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 "/>
                        <StackLayout Orientation="Vertical">
                            <Label Text="ST:" /><Label Text = "{Binding ST_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                            <Label Text="Folio:" /><Label Text = "{Binding Folio_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                                <Label Text="txt" /><Label Text = "{Binding Sent} " FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40"/>
                        </StackLayout>

                    </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

And in my ItemsPage.xaml.cs I can't access to my StackLayout with Name="General" , I need paint this with color, but I can't, please help. 在我的ItemsPage.xaml.cs我无法使用Name="General"访问我的StackLayout ,我需要用颜色绘制它,但是我不能,请帮忙。

In general I can't do General.Background, I don't know access this. 通常,我不能执行General.Background,我不知道访问此方法。

Thanks! 谢谢!

Elements inside a DataTemplate are not accessible from outside the DataTemplate ; 无法从DataTemplate外部访问DataTemplate内部的元素; this includes the code-behind (the xaml.cs file). 这包括后台代码( xaml.cs文件)。

DataTemplates are handled in a special way. DataTemplates以特殊方式处理。 They're used as a template (hence the name) for each item inside the ListView . 它们用作ListView内每个项目的模板(因此而得名)。 This means that at runtime there's going to be an instance of the contents inside the DataTemplate for each item. 这意味着在运行时,每个项目的DataTemplate内将有一个内容实例。 If you have 20 items in that list, there's going to be 20 StackLayout s with the name General . 如果该列表中有20个项目,那么将有20个StackLayout ,名称为General You can read about DataTemplates in the docs . 您可以在docs中了解有关DataTemplates的信息。

If you want to set the background color of the StackLayout , the easiest way is to do that directly on the StackLayout element: 如果要设置StackLayout的背景色,最简单的方法是直接在StackLayout元素上进行StackLayout

<StackLayout x:Name="General" BackgroundColor="Blue" Orientation="Horizontal"...

Alternatively you can create a ContentView and put that inside the ViewCell . 或者,您可以创建一个ContentView并将其放在ViewCell The BindingContext of the ContentView will automatically be set to the current item. ContentViewBindingContext将自动设置为当前项目。 ContentView s are a bit like ContentPage s, but you can use them inside a page like any other View (like a Button or a BoxView . ContentView有点像ContentPage ,但是您可以像其他任何View一样在页面内使用它们(例如ButtonBoxView

Edit 编辑

To add a ContentView right-click and add a new file, choose ContentView . 要右键单击添加ContentView并添加新文件,请选择ContentView Put the XAML inside ViewCell inside the ContentView : 将XAML放在ContentView内的ViewCell内:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Testing.MyView">
    <ContentView.Content>
        <StackLayout x:Name="General" Orientation="Horizontal" HorizontalOptions="Fill" Padding="5">
            <Image Source="{Binding FileName, Converter={StaticResource ImageConverter}}" HeightRequest="150" WidthRequest="150" AbsoluteLayout.LayoutBounds="250.25, 0.25, 50, 50 " />
            <StackLayout Orientation="Vertical">
                <Label Text="ST:" />
                <Label Text="{Binding ST_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
                <Label Text="Folio:" />
                <Label Text="{Binding Folio_string}" FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
                <Label Text="txt" />
                <Label Text="{Binding Sent} " FontSize="24" AbsoluteLayout.LayoutBounds="0.25, 0.25, 400, 40" />
            </StackLayout>
        </StackLayout>
    </ContentView.Content>
</ContentView>

In the code-behind, you can access all controls: 在后面的代码中,您可以访问所有控件:

public partial class MyView : ContentView
{
    public MyView()
    {
        InitializeComponent();

        General.BackgroundColor = true ? Color.Blue : Color.Brown;
    }
}

Then add the ContentView to the ViewCell : 然后将ContentView添加到ViewCell

<ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <local:MyView/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM