Hi Xamarin Forum,
Can somebody help me here im little stuck at rendering my other images from my database Here is my Code
PostModel.cs
public class Post
{
public int Id { get; set; }
public string Uploader { get; set; }
public string OwnersEmail { get; set; }
public DateTime dateUpload { get; set; }
public string PhotoSource { get; set; }
public string PhotoSource2 { get; set; }
public string PostContent { get; set; }
public string OwnersPhoto { get; set; }
public ImageSource Photo
{
get
{
byte[] bytes = Convert.FromBase64String(PhotoSource);
MemoryStream ms = new MemoryStream(bytes);
ImageSource s = ImageSource.FromStream(() => ms);
return s;
}
set
{
}
}
public ImageSource Photo2
{
get
{
byte[] bytes = Convert.FromBase64String(PhotoSource);
MemoryStream ms = new MemoryStream(bytes);
ImageSource s = ImageSource.FromStream(() => ms);
return s;
}
set
{
}
}
}
Xaml
<StackLayout>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackLayout>
<StackLayout Spacing="0" VerticalOptions="Start" Orientation="Horizontal">
<StackLayout.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,12,0,0" />
<On Platform="Android" Value="0,15,0,0" />
</OnPlatform>
</StackLayout.Margin>
</StackLayout>
</StackLayout>
</Grid>
<Frame HorizontalOptions="Fill" Grid.Column="0" Grid.Row="0" HeightRequest="20" BackgroundColor="Black" Margin="0,-25,0,25">
<Image Source="back_white.png" HeightRequest="40" WidthRequest="40" HorizontalOptions="Start">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="BackFunction"/>
</Image.GestureRecognizers>
</Image>
</Frame>
<StackLayout Margin="10,5,10,0">
<StackLayout>
<Image Source="{Binding Photo}" HeightRequest="200" Aspect="AspectFill" />
<StackLayout Orientation="Horizontal" Margin="5,5,10,10">
<Image Source="{Binding Photo_2}" HeightRequest="70" WidthRequest="70" Margin="0,0,5,0" />
<!--<Image Source="{Binding Photo3}" HeightRequest="70" WidthRequest="70" Aspect="AspectFill" Margin="0,0,5,0" />
<Image Source="{Binding Photo4}" HeightRequest="70" WidthRequest="70" Aspect="AspectFill" Margin="0,0,5,0" />-->
</StackLayout>
</StackLayout>
<Label Text="{Binding HouseName}" HorizontalOptions="Start" FontSize="16"/>
<Label Text="Details" Margin="0,15,0,0"/>
<ScrollView>
<StackLayout>
<Label Text="{Binding PostContent}" FontSize="15"/>
<StackLayout Orientation="Horizontal">
<Label Text="Contact"/>
<Label Text="{Binding Uploader}" TextColor="Blue" x:Name="ownerName">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="ContactOwner"/>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</StackLayout>
</ScrollView>
</StackLayout>
</StackLayout>
CodeBehind
public partial class PostDetailPage : ContentPage
{
Post SelectedPost;
public List<Post> postPhotos;
public PostDetailPage (Post PostItem)
{
this.SelectedPost = PostItem;
InitializeComponent ();
BindingContext = SelectedPost;
BindPhoto();
}
private void BindPhoto()
{
DataSet ds = new DataSet();
Pipeline databaseConnect = new Pipeline();
try
{
using (SqlCommand selectPhoto = new SqlCommand("SELECT * FROM t_Post WHERE Id='" + SelectedPost.Id + "'", databaseConnect.connectDB()))
{
using (SqlDataReader reader = selectPhoto.ExecuteReader())
{
postPhotos = new List<Post>();
while (reader.Read())
{
string image2 = Convert.ToBase64String((byte[])reader["Photo_2"]);
var otherPhoto = new Post
{
PhotoSource2 = image2
};
postPhotos.Add(otherPhoto);
}
}
}
}
catch(Exception ex)
{
}
}
}
}
PS: what happens is that if Im gonna use parameter Photo everything goes well but if I use Photo_2 my program breaks and says
System.InvalidCastException: Specified cast is not valid