add web page in xamarin form - xamarin

I'm very very new to using Xamarin forms. So new in fact I have just watched the "microsoft xamarin forms for absolute beginners".
I'm trying to include a webpage into the Xamarin form but the only info I can find is
var browser = new WebView {
Source = "http://xamarin.com"
};
This might sound silly but has anyone got a full tutorial on how to include one?
thanks

just create a WebView and assign it to a ContentPage. Then you can either make this page the main page of your app (in the App class) or you can navigate to it from another page.
ContentPage page = new ContentPage {
Content = new WebView { Source = "http://xamarin.com" }
};

To display a website from the internet, set the WebView's Source property to a string URL:
var browser = new WebView
{
Source = "http://xamarin.com"
};
see this:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows#performance

Related

How to navigate from parent project's page to a child project's page in Xamarin

I am working with xamarin forms and my solutions has 4 project in that one is commmon code and others for android, ios and uwp. Now when i run the solution using uwp emulator then it runs the first page of common code which is set in the common code app.xaml.cs but i want to set the start page from uwp project.Is there any way to set the start page in common code to a page which is present in the uwp project.
Well, that is quite simple in your UWP application look for the App.xaml.cs class.
In that look for something like below:
if (e.PrelaunchActivated == false)
{
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
}
Here rootFrame.Navigate(typeof(MainPage), e.Arguments); is the piece of code which decides what page to load changing this to another page will do the trick
Good luck, Feel free to get back if you have queries
Did you try in shared code this one?
if (Device.RuntimePlatform == Device.WPF)
{
MainPage = new NavigationPage(new WpfStartPage());
}
else
{
MainPage = new NavigationPage(new StartPage());
}
UWP update
if (Device.RuntimePlatform == Device.UWP)
{
MainPage = new NavigationPage(new WpfStartPage());
}
else
{
MainPage = new NavigationPage(new StartPage());
}

iOS Native to Forms navigation

I have been working on an application that uses both forms and native in Xamarin. The app starts with the form and switch to native to open the camera. My problem is once the image is clicked I want to go back to Forms application but I am unable to find enough documentation on it.
I have tried Navigation.PushAsync in iOS but it doesnt work.
var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);
var jpegImage = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer);
var photo = new UIImage(jpegImage);
byte[] data;
NSData imageData = photo.AsJPEG();
data = new byte[imageData.Length];
EnrollImageDisplay.ImageData = data;
Where EnrollImageDisplay is my content page in forms. Not I have to call this page but I am unable to do so. Any solution with this?
EDIT: Solved it using Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new Page());

XAMARIN: WebView Loads Links Half-Way Down the Page (or keeps position?)

I've got this code for creating a WebView:
public class SessionsActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.discover);
//declare webview and tell our code where to find the XAML resource
WebView discoverWebView = FindViewById<WebView>(Resource.Id.webViewDiscover);
//set the webview client
discoverWebView.SetWebViewClient(new WebViewClient());
//load the subscription url
discoverWebView.LoadUrl("https://www.bitchute.com/");
//enable javascript in our webview
discoverWebView.Settings.JavaScriptEnabled = true;
//zoom control on? This should perhaps be disabled for consistency?
//we will leave it on for now
discoverWebView.Settings.BuiltInZoomControls = true;
discoverWebView.Settings.SetSupportZoom(true);
//scrollbarsdisabled
// subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
discoverWebView.ScrollbarFadingEnabled = false;
}
}
but when I click on links inside my app, the page loads at the same spot as the last page was, which makes navigation very difficult, because mobile pages are supposed to load from the top, not from the middle. So basically, if I am in my subscriptions feed, and then I scroll down to find more content, I click on a user, and their video page loads videos from weeks ago, because it loaded with the same view elevation as the root page.
The site I'm using WebView with is also designed for mobile users, if that matters. The weird thing is that this used to not happen until recently... don't know what changed. How can I have the WebView load at top of page on each click?
I'm using TabHost for this .. here's my full code for context
https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.68/MainActivity.cs
thanks in advance!

Do I create a new Navigation Page or Content Page when making a tabbed application?

I want to create an application with tabs. I don't need the functionality of a Navigation page that allows me to go back to the last screen. I just want the tab bar to allow me to select one of five pages.
Here's the code I have so far:
public partial class MainPage : TabbedPage
{
public MainPage()
{
InitializeComponent();
My question is which of the following should I use. Note that HomePage inherits from ContentPage.
// this is the one the app uses now. Do I really need to NavigationPage and then inside that another page HomePage?
var homePage = new NavigationPage(new HomePage())
{
Title = "Home",
Icon = "ionicons_2_0_1_home_outline_25.png"
};
// I thought this would be better but ContentPage constructor cannot take an argument
var homePage = new ContentPage(new HomePage())
{
Title = "Home",
Icon = "ionicons_2_0_1_home_outline_25.png"
};
// this is my latest thought but would like to hear from others
var homePage = new HomePage()
{
Title = "Home",
Icon = "ionicons_2_0_1_home_outline_25.png"
};
Children.Add(homePage);
It's recommended that a TabbedPage should be populated with
NavigationPage and ContentPage instances only. This will help to ensure
a consistent user experience across all platforms.
The quote above is from the official TabbedPage documentation.
You don't have to wrap a ContentPage by NavigationPage if it is not needed.
If HomePage is inheriting from a ContentPage then you can just create an instance of it and add to children the next way:
var homePage = new HomePage
{
Title = "Home",
Icon = "ionicons_2_0_1_home_outline_25.png"
};
Children.Add(homePage);
P.S.: Official documentation has covered navigation topics quite nicely. Please get familiar with it.

Xamarin forms background image for all Pages

It´s possible to set an image to be the background of all Pages on my application (for all platforms IOS, Android and WP) ?
Page has a BackgroundImage property, so I can create a base page, set the background and make all my pages extends from it.
I want to know if there is a more appropriate solution, to handle this.
You could use Styles in your App.cs :
public App ()
{
Application.Current.Resources.Add(new Xamarin.Forms.Style(typeof(Page)){
Setters = {
new Xamarin.Forms.Setter { Property = Page.BackgroundImageProperty, Value = "back.png"},
}
});
}
much less preferred or advised but for the sake of showing another option you could attach an event handler to your NavigationPages and set the background from there:
yourRootNavigationPage.Pushed+= (sender, e) => e.Page.BackgroundImage = "back.png";

Resources