Xamarin.Forms receiving an image from an api endpoint - c#

I am trying to pull an image from an api endpoint. So far everything works an appears except the image. I am not sure if its because I am calling it as a string or if i should change it to something else.
Below is my code, if more is needed or if im missing something please let me know.
Thank you
EventsDAL.cs
public class Events
{
public string EventTitle { get; set; }
public string EventDescription { get; set; }
public DateTime EventDateAndTime { get; set; }
public string EventPosterImageURL { get; set; }
public string EventTitleReturn { get { return EventTitle; } }
public string EventDescriptionReturn { get { return EventDescription; } }
public DateTime EventDateReturn { get { return EventDateAndTime; } }
public string EventImageReturn { get { return EventPosterImageURL; } }
}
EventsPage.xaml
<ScrollView >
<ListView x:Name="lvEvent" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout VerticalOptions="Fill" >
<Label x:Name="lbTitle" HorizontalOptions="Center" Text="{Binding EventTitleReturn}" FontSize="Title"/>
<Label Text="{Binding EventDescriptionReturn}" FontSize="Large"/>
<Label Text="{Binding EventDateReturn}" HorizontalOptions="Center" FontSize="Large"/>
<Image Source="{Binding EventImageReturn}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollView>
My controller
public ActionResult Events()
{
var umbEvents = Umbraco.Content(1056).Children<Event>().ToList();
var vmEvents = new List<EventViewModel>();
foreach (var item in umbEvents)
vmEvents.Add(new EventViewModel
{
EventTitle = item.EventTitle,
EventDescription = item.EventShortDescription,
EventDateAndTime = item.EventDateAndTime,
EventPosterImageURL = item.EventPosterImage == null ? "" : item.EventPosterImage.Url,
EventURL = item.Url
});
return Json(vmEvents, JsonRequestBehavior.AllowGet);
}
Postman
"EventTitle": "Seeing Red!",
"EventDescription": "The Ladies of Seeing Red are one of the best acoustic bands on the Memphis music scene.  They provide a great variety of music for any environment.  It is a fun show with a great energy level and they adapt well to any size venue. Sponsored by ProBuilt of Memphis.",
"EventDateAndTime": "/Date(1599332400000)/",
"EventURL": "/events/seeing-red/",
"EventPosterImageURL": "/media/3ablnzez/seeingred.jpg"

"EventPosterImageURL": "/media/3ablnzez/seeingred.jpg"
this is a path, not a url. If you want the image to load from the remote server, you need to return a complete url - ie, http://myserver.com/media/3ablnzez/seeingred.jpg

Related

Xamarin.Forms update Entry value from Xamarin.Essentials.Contacts selection using MVVM

I'm trying to update the value of an Entry control using Xamarin.Essentials.Contacts.
I have a PhoneNumber Entry that's empty when opening the page but there's a button below it that says 'Select Contact' and I'm retrieving the phone number and am trying to set the value of the PhoneNumber Entry to that phone number.
I can't seem to be able to do it and I don't really know how to do it.
When I select the contact I get the number but it doesn't update the Entry value.
Here's my code
XAML
<StackLayout Orientation="Horizontal"
Padding="16,12"
Spacing="12">
<Frame HorizontalOptions="FillAndExpand"
Padding="6, 1"
Margin="5, 0"
CornerRadius="8"
BackgroundColor="#212121">
<Entry x:Name="PhoneNumberEntry"
Text="{Binding PhoneNumberEntry, Mode=TwoWay}"
Placeholder="Don't add '+1'"
Keyboard="Numeric"
MaxLength="10"
Style="{StaticResource EntryStyle}" />
</Frame>
</StackLayout>
<StackLayout Orientation="Horizontal"
Padding="16,12"
Spacing="12">
<Button x:Name="ContactButton"
Text="Or select contact"
Command="{Binding SelectContactCommand}"
BackgroundColor="Red"
HorizontalOptions="FillAndExpand" />
</StackLayout>
ViewModel
public class InviteViewModel : BaseViewModel
{
public InviteViewModel()
{
SelectContactCommand = new Command(async () => await ExecuteSelectContactCommand());
Info = new StringBuilder();
}
public string PhoneNumberEntry { get; set; }
public Command SelectContactCommand { get; set; }
public StringBuilder Info { get; set; }
private async Task ExecuteSelectContactCommand()
{
try
{
var contact = await Contacts.PickContactAsync();
if (contact == null)
{
return;
}
Info.AppendLine(contact.Phones.FirstOrDefault()?.PhoneNumber ?? string.Empty);
var phoneNumber = Info.ToString().Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").TrimEnd();
PhoneNumberEntry = phoneNumber;
}
catch (Exception ex)
{
}
}
}
Like #Jason and #AppPack said, it was only a matter of using OnPropertyChanged() in the setter of PhoneNumberEntry
private string phoneNumberEntry;
public string PhoneNumberEntry
{
get { return phoneNumberEntry; }
set
{
phoneNumberEntry = value;
OnPropertyChanged("PhoneNumberEntry");
}
}

Can't insert data from Sqlite in ListView

I'm trying to insert certain pieces of data of a user into a ListView, but can't seem to get any of them to show up.
This is how my code looks like so far. The issue is in the last two classes (Invite_Page.xaml.cs and Invite_Page.xaml), but I also pasted my notes from other classes which could be relevant.:
User.cs:
public class User {
public string fName { get; set; }
public string lName { get; set; }
public string uName { get; set; }
public string Pw { get; set; }
// public string cls { get; set; }
}
fName and lName are both Strings
DatabaseManager.cs:
public class DatabaseManager
{
SQLiteConnection dbConnection;
public DatabaseManager()
{
dbConnection = DependencyService.Get<IDBInterface>().CreateConnection();
}
public List<User> GetAllUsers()
return dbConnection.Query<User>("Select * From [User]");
public int SaveUser(User aUser)
{
//dbConnection.CreateTable<User>();
//return 1;
return dbConnection.Insert(aUser);
}
public List<User> GetAllNames()
return dbConnection.Query<User>("Select fName, lName From [User] ORDER BY fName;");
//.....
Invite_Page.xaml:
<ListView
x:Name="invitees"
SeparatorColor="White"
BackgroundColor="Transparent"
ItemsSource="{Binding invitees}"
IsGroupingEnabled="true"
IsPullToRefreshEnabled="true"
ItemTapped="Handle_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding}" TextColor="White"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Invite_Page.xaml.cs:
DatabaseManager DBM = new DatabaseManager();
// List<User> Invitees(String SearchText = null)
// {
// invitees.ItemsSource = DBM.GetAllNames();
// return Invitees();
// }
protected override void OnAppearing()
{
base.OnAppearing();
invitees.ItemsSource = DBM.GetAllNames();
}
In the Invite_Page.xaml.cs code, you can see that I tried inputting the data by using the code that is commented out. The result of that was that nothing showed up in the list. But with the second method which isn't commented out, the result was that "RoseySports.User" was placed in the ListView. I'm trying to make it so that the values of fName and lName are put together as one string, and are placed in the ListView.
The easiest way is to override ToString() method of the User class:
public override string ToString()
{
return fName + " " + lName;
}
The other option is to build more complicated DataTemplate as #ViralThakker mentioned.
P.S. The line ItemsSource="{Binding invitees}" in your XAML is unusable if you are assigning ItemsSource in code.
First, you have to specify what you really want from the model of User to the TextCell that you use binding. You set as ItemSource Users, but you just set TextCell Binding without any variable. Try to set Text={Binding fName} for example.
Second, try to use ViewModel to populate data. Learn more here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm
You need to create Custom List item template to display value from your object.
For example you want to display fName,lName and uName then you'll have to use below code for binding values to list item
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="10">
<Label Text="{Binding fName}" TextColor="White"/>
<Label Text="{Binding lName}" TextColor="White"/>
<Label Text="{Binding uName}" TextColor="White"/>
</StackLayout>
</ViewCell>
</DataTemplate>

Xamarin Forms + Azure Reading Column

I just dived in to Xamarin Forms and using Azure.
I am able to successfully call the row from the database but it's displaying something into the ListView instead of what I want it to display.
My codes:
private async void generateList()
{
var clientUsers = await App.MobileService.GetTable<iSoftTicket.Model.TblUser>().Where(u => u.uc_category == "Client").ToListAsync();
lvClient.ItemsSource = clientUsers;
}
I want it to display the column with Gulfden on it. Any help is appreciated.
Thanks.
You should create your ListView's ItemTemplate(e.g. ViewCell) and then add some label to display your data like:
<ListView x:Name="MyListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Admin}"/>
<Label Text="{Binding Tickets}"/>
<Label Text="{Binding Clients}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I notice that you have set your listview's ItemSource, please make sure your model's property names is Corresponding to the Binding name in the XAML, you can refer to my sample in code behind:
ObservableCollection<MyModel> list = new ObservableCollection<MyModel>();
for (int i=0; i<10; i++)
{
list.Add(new MyModel { Admin = "admin" + i, Clients = "client", Tickets = "tickets" });
}
MyListView.ItemsSource = list;
public class MyModel
{
public string Admin { set; get; }
public string Tickets { get; set; }
public string Clients { get; set; }
}

Xamarin forms save switch change to sqlite

I am new to xamarin and I am stuck with a problem.
I am making a todo app, where in the listview where you see the items you could set the task to done with a switch. The switch currently displays the saved value, so it is bound to the value from the database, but I can not see how to save the state if it gets changed.
As I see somehow I need to get the object from the switch, I appreciate the help.
<ListView x:Name="MainListView"
ItemSelected="MainListView_OnItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Content}"></Label>
<Switch IsToggled="{Binding Done}" HorizontalOptions="EndAndExpand"></Switch>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is my custom ViewcCel where the value is bound.
public ToDoItem() { }
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Content { get; set; }
public string PicturePath { get; set; }
public bool Done { get; set; }
This is the Entity.
The list is on the main page, with the page in the code behind i do other stuff, just in case here is the code https://github.com/benonymus/ToDoApp3
You can have:
<Switch x:Name="my_switch" Toggled="Switch_Toggled" />
and in page:
public MyPage()
{
my_switch.IsToggled = Done;
}
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
Done = my_switch.IsToggled;
}
Edit:
You can also try:
<Switch IsToggled="{Binding Done, Mode=TwoWay}" />

DataTemplate x:Bind namespace error

I have a question about Windows 10 UWP development using Visual Studio 2015.
I'm trying to use a DataTemplate for my GridView according to this tutorial. The problem I'm having is with my namespace.
I am not allowed to share my exact code for obvious reasons, but I'm wondering if one of you guys might have run into this before. I am getting almost the same error as this person (error code 0x09c4), except my DataTemplate is in my code-behind-file, not global like him/her. Along with that error I'm also getting the illusive "the _name does not exist in the namespace _namespace".
Here is a piece of my xaml file:
<Grid>
...
<GridView ItemsSource="{x:Bind ViewModel.AssessExItems}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:AssessExItem">
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
I know the DataTemplate is empty but even if I enter something there it still doesn't work. Here is my code-behind-file for this xaml file:
public sealed partial class AssessmentExample1Screen : Page
{
public AssessExItemViewModel ViewModel { get; set; }
public AssessmentExample1Screen()
{
this.InitializeComponent();
this.ViewModel = new AssessExItemViewModel();
}
}
public class AssessExItem
{
public int _assessment_id { get; set; }
public string name { get; set; }
public string surname { get; set; }
public string date { get; set; }
//public EmpAssessItem() { }
}
public class AssessExItemViewModel
{
private ObservableCollection<AssessExItem> exampleItems = new ObservableCollection<AssessExItem>();
public ObservableCollection<AssessExItem> AssessExItems { get { return this.exampleItems; } }
public AssessExItemViewModel()
{
//for (int i = 1; i < 3; i++)
//{
this.exampleItems.Add(new AssessExItem()
{
name = "Cat 777",
surname = "Botha",
date = "2015-03-22"
});
//}
this.exampleItems.Add(new AssessExItem()
{
name = "XZR 678",
surname = "Botha",
date = "2015-03-22"
});
this.exampleItems.Add(new AssessExItem()
{
name = "TBL 123",
surname = "Botha",
date = "2015-03-22"
});
}
}
Please help.
I reproduced your problem.
How to solve : Clean and build or rebuild the solution.And then I tested it,it works.
I guess the most possible reason of why it happened is build can update the file mainpage.g.cs which determined where to find the datatype.
<GridView ItemsSource="{x:Bind ViewModel.AssessExItems}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:AssessExItem">
<StackPanel Height="100" Width="100" Background="OrangeRed">
<TextBlock Text="{x:Bind name}"/>
<TextBlock Text="{x:Bind surname}" x:Phase="1"/>
<TextBlock Text="{x:Bind date}" x:Phase="2"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>

Categories

Resources