Xamarin,尝试访问Listview中的标签时出现错误,该名称在当前上下文中不存在

时间:2019-07-17 17:14:11

标签: listview xamarin

我想通过.cs文件访问Listview中的标签,但是给我一个错误“该名称在当前上下文中不存在”。

当我从Listview中取出标签时,它可以工作,但在listview-grid内却出现错误。我还尝试在listview内强制标签中的文本,并且它可以工作。只是当我尝试通过.cs文件访问时。

//error
rain1logLabel.Text = "test";

//I added a class file for the source of databinding as suggested by Jason
public string rain1Lbl
{
set => V;     
} 

//now going to access it in xaml file
<Label Grid.Column="0" Text="{Binding rain1Lbl}" />

//adding none gibberish get
private string rain1lbl = "test";  //this one is shaded like im not using it

public string rain1Lbl
{
get => rain1lbl;

set
{
    rain1lbl = value;
}

}

//xaml file,  no errors but it's empty on UI
<Label Grid.Column="0" Text="{Binding rain1Lbl}" />

//this is the listview
<ListView x:Name="postListView" >
<ListView.ItemTemplate>
         <!-- from the post.cs -->
    <DataTemplate>
        <ViewCell>
            <Grid HorizontalOptions="Center" VerticalOptions="Center" >
                <Grid.RowDefinitions> <!-- 8 rows -->
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <!-- drain1 Row 1 -->
            <Grid Grid.Row="1" HorizontalOptions="Center" >
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="50"/>
            </Grid.ColumnDefinitions>

            <Label Grid.Column="1" Text="{Binding rain1vol}" />
            <Label Grid.Column="0" Text="{Binding rain1Lbl}" />
            </Grid>   <!-- end  Row 1 --> 

                            <!--  Row 2 -->
            <Grid Grid.Row="2" HorizontalOptions="Center" >
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="50"/>
            </Grid.ColumnDefinitions>


            <Label Grid.Column="1" Text="{Binding rain2vol}" />
            <Label Grid.Column="0" Text="nothing" />
            </Grid>   <!-- end Row 2 -->         
            </Grid>
        </ViewCell>
    </DataTemplate>

</ListView.ItemTemplate>

//this is the source of Binding
public class Post
{
    //private const string V = "test";
    //string rain1lbl = Settings.Drain1LocationSettings;
    private string rain1lbl = "test";
    //course51

    //ID primary key that we will autoincrement
    //These are columns ID,   8 - 9 columns

    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public string rain1Lbl
    {
        get => rain1lbl;


        set
        {
            rain1lbl = value;
        }
    }

    [MaxLength(3)]
    public string rain1vol { get; set; }

    [MaxLength(3)]
    public string rain2vol { get; set; }


}
}

//and this is the .cs file nothing really going on there yet
public partial class HistoryLogPage : ContentPage
{
    public HistoryLogPage()
    {
           InitializeComponent();

    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        //this  is where we are going to read from the database


        //"using" will close connection automatically because it will only run this block of code
        SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
        {
            //create table it will create table if table doesnt exist if it exist then ignore this line
            conn.CreateTable<Post>();

            //create new variable, need to use using.linq to use ToList to list query in database
            //ToList will turn the table query into list

            var posts= conn.Table<Post>().ToList();
            //the post list is going to be the source for the data binding

            //data context ; each element on post will a row will be added
            postListView.ItemsSource = posts;


            conn.Close();
            //right click quickwatch
        }
    }


}

}

enter image description here

0 个答案:

没有答案