Show image from URL with Xamarin.Forms - c#

I am using this code to show image from an URL
.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="LandAHand.VolunteerView">
<ContentPage.Content>
<AbsoluteLayout BackgroundColor="Maroon">
<Image x:Name="backgroundImage" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill" />
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace LandAHand
{
public partial class VolunteerView : ContentPage
{
public VolunteerView()
{
InitializeComponent();
backgroundImage.Source = new UriImageSource
{
Uri = new Uri("https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg"),
CachingEnabled = true,
CacheValidity = new TimeSpan(5, 0, 0, 0)
};
}
}
}
This code is successfully working with iOS but it is not working with Android.

Well you can do this thing easier with your Xaml just make your xaml like this
<Image x:Name="backgroundImage" Source="https://s9.postimg.org/aq1jt3fu7/handshake_87122244_std.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Aspect="AspectFill"/>
And remove the code related in the code behind. The cashing is enabled by default for 24 hours

It's not working because you use a https url.
For fix it, you should be configure your Android Project like this :
In Project Options > Android Options, click Advanced Options
HttpClient implementation : choose Android
SSL/TLS implementation : choose Native TLS 1.2+
https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=windows
And update all Xamarin.Android packages

Related

Using PHP / Mysql in Xamarin for Login and Redirect to another XAML

I just started to use Xamarin. Now i have learnt how to create Cross Platform Apps. But now i have a Worry. I want to get input from the input Box as this
Here is what my XAML looks like :
<?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="Qasimapp.Page1"
BackgroundColor="#fff">
<ContentPage.Content>
<StackLayout Spacing="20" Padding="50" VerticalOptions="Center">
<Image Source="slim_logo.png" x:Name="logom" HeightRequest="100" WidthRequest="100"/>
<Entry Placeholder="Username"></Entry>
<Entry Placeholder="Password" IsPassword="True"></Entry>
<Button Text="Log In" TextColor="White" BackgroundColor="#ff77D065"></Button>
</StackLayout>
</ContentPage.Content>
</ContentPage>
And use this to connect to a database using WebAPI or something like a normal PHP API and redirect to another form such as the Dashboard. here is what my PHP looks like
<?php
require_once('inc/config.php');
$con = mysqli_connect($host,$user,$pass,$db) or die ('Cannot Connect to Database'.mysqli_error());
$username = mysqli_real_escape_string($con,$_POST['userID']);
$passwd = mysqli_real_escape_string($con,$_POST['passwd']);
$designation = mysqli_real_escape_string($con,$_POST['designation']);
$query = "SELECT * FROM evmsdbusers WHERE username='$username' AND password='$passwd' and designation='$designation'";
$result = mysqli_query($con,$query);
if (mysqli_num_rows($result) == 1) {
if($designation == 'User')
{
saveLoginLogs();
}
else if($designation =='Security')
{
saveLoginLogs();
}
}
?>
Now i have uploaded it to a server on here http://32.125.23.120/login.php for instance and i want to use it to login using XAML cross platform
How can I achieve something like this? How can it Authenticate and then redirect to the main Page i want it to? I am very new to something like this and all my search prove abortive .

Target Invocation exception in Xamarin Forms Android when using french culture with font attributes

I used the labels in my xamarin page and set FontAttributes as like below code in my xaml page. By default (US culture) no issues. When i change my android device culture as french i am getting System.Reflection.TargetInvocationException. If i remove FontAttributes, it is working fine. I can't debug or get details from output window. Anyone please help on this.
My Code
<Label x:Name="continentLabel" FontAttributes="Bold"/>
I created a simple xamarin forms project and edited the xaml file as you have posted, I also added two RESX files:AppResources.resx and AppResources.fr.resx, then I set the simulator's language setting to French. It works all right. I think you may missed something, so I post my code here:
MainPage.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"
xmlns:local="clr-namespace:App34"
x:Class="App34.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Label x:Name="String1"
FontAttributes="Bold"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App34
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
String1.Text = AppResources.String1;
}
}
}

Xamarin Start-up

I am a newbie in Xamarin. I created a C# cross-platform project, after the project has been created there were several errors I don't know why these errors occurred. How can I fix these errors? When I click build nothing displays. When I click Play my default app does not display.
App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.App">
<Application.Resources>
</Application.Resources>
</Application>
MainPage.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"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
}
}
Try to reinstall the Xamarin with Visual studio , It seems there has been something missing form installation. You can find the documentation here
https://learn.microsoft.com/en-us/xamarin/cross-platform/get-started/installation/windows
Regards
Right click on your project
Manage Nuuget Packages
Installed tab : Check if Xamarin is intalled, if yes update it, if not install xamarin
Clean and rebuild your project.

No property found for TickFrequency in <Slider> in XAML on Xamarin

I am working in Xamarin Studio, trying to learn C# and XAML and I am having some trouble with a bit of XAML for a simple program. I am trying to use a and change the TickFrequency, but it doesn't seem to recognize it or any Tick related attributes. Here is my code:
<?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="HelloWorld.GreetPage">
<ContentPage.Padding>
<OnPlatform x:TypeArguments="Thickness"
iOS="0, 20, 0, 0">
</OnPlatform>
</ContentPage.Padding>
<StackLayout
BindingContext = "{x:Reference slider}">
<Label
HorizontalOptions="Start"
Text="{Binding Value,
StringFormat='Font Size: {0:F0}'}"/>
<Slider
x:Name="slider"
Maximum = "64"
Minimum = "16"
TickFrequency="1"/>
</StackLayout>
</ContentPage>
The error I receive says "No property, bindable property, or event found for "TickFrequency"
Thank you in advance!
For adding ticks in xamarin forms in android and iOS apps, you can try this

Xamarin forms navigation

I am having trouble with Xamarin forms navigation, as in, it does not work at all.
My code :
appname.cs
using System;
using Xamarin.Forms;
namespace List
{
public class App : Application {
public App () {
MainPage = new NavigationPage (new HomePage ());
}
}
}
homepage.xaml.cs
using System;
using Xamarin.Forms;
using System.ComponentModel;
using System.Globalization;
namespace List
{
public partial class HomePage : ContentPage {
public HomePage () {
InitializeComponent ();
}
public void OnButtonClicked (object sender, EventArgs args) {
Navigation.PushAsync (new DetailsPage ());
}
}
}
My intention is obviously to open a main page, have a button on there that goes to a second page upon press.
However this happens instead whenever the button is clicked:
error message
Could not load type 'UIKit.UIView_UITextField' from assembly 'Xamarin.Forms.Platform.iOS'.
Can someone tell me what I'm doing wrong here?
Edit - code for DetailsPage, since this could be source of error:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace List
{
public partial class DetailsPage : ContentPage
{
public DetailsPage ()
{
InitializeComponent ();
}
}
}
Edit 2 - code for Xaml pages (thanks for the help everyone, I really appreciate it):
Homepage.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" xmlns:local="clr-namespace:List" x:Class="List.HomePage">
<ContentPage.Title>
1
</ContentPage.Title>
<Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" />
</ContentPage>
DetailsPage.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="List.DetailsPage">
<Label Text="LOL"/>
</ContentPage>
In your xaml, the first element needs to be a layout (if you have multiple elements). I would try something similar to below and see if anything changes.
HomePage.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" xmlns:local="clr-namespace:List" x:Class="List.HomePage" Title="1">
<StackLayout>
<Button Text="Click Me!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" Clicked="OnButtonClicked" />
</StackLayout>
</ContentPage>
DetailsPage.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="List.DetailsPage">
<StackLayout>
<Label Text="LOL"/>
</StackLayout>
</ContentPage>
Also, I noticed you were setting your title like using ContentPage.Title 1 ContentPage.Title. This is unnecessary. Just set the title directly in your ContentPage tag as I showed in the above example.
Well, I was having the same problem as you. I came across this solution of the xamarin guide.
It seems that in app.cs you need to call the using Xamarin.Forms.Xaml;
Right above your namespace insert [assembly:XamlCompilation(XamlCompilationOptions.Compile)]
But I really can't tell why. I'm Looking for the reason. Try to see if it resolves. In my case went well. Sorry about the bad english and the bad edit of the post, kind of new to this. But comparing all your code, it was missing this piece for you too, maybe this is the reason.
EDIT (12/01/2017) - Add
i found this, that might explain why was not working, seems like the function that we are using is part of a new set for Xamarin that helps him to became faster.
I can be wrong, if I am, please correct me someone that knows better.
Navigation.PushModalAsync(new DetailsPage());
Reference: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/introduction-to-xamarin-forms/#Navigation

Categories

Resources