Issue
I don't know why I can't access the x:Name of the list in xaml file from the code behind any help plz?
I'm new to Xamarin and Im trying to build a basic project ,I tried to google some solutions but I found that my code is correct so I don't what to do now .
here the xml file Posts.Xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="FavPosts.Posts">
<ListView x:Name="listview" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
Padding="5">
<Label HorizontalOptions="StartAndExpand"/>
<Button Text="Favorite"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
and the code behind is Posts.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace FavPosts
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Posts : ContentPage
{
public Posts()
{
InitializeComponent();
}
public class Post
{
public string Status { get; set; }
public string Details { get; set; }
}
List<Post> Posty = new List<Post> {
new Post { Status="f1", Details="D1" },
new Post { Status="f2", Details="D2"}
};
listview.ItemsSource = Posty;
}
}
Solution
You should place class Post outside from Posts! To create List "automatically" you should place it in Constructor. But your main issue ist that class Post need to be outside
namespace FavPosts
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Posts : ContentPage
{
public Posts()
{
InitializeComponent();
List<Post> Posty = new List<Post> {
new Post { Status="f1", Details="D1" },
new Post { Status="f2", Details="D2"}
};
listview.ItemsSource = Posty;
}
}
public class Post
{
public string Status { get; set; }
public string Details { get; set; }
}
}
Answered By - PeterN
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.