Load image from solution in WebView Xamarin Forms - webview

I have inside ScrollView images and i want selected image opened in some new page and have pinch and zoom , i try webview control but i cant load image from my solution path .
void loadhtml()
{
var htmlSource = new HtmlWebViewSource {
Html = "<!DOCTYPE html>\n<html>\n<body>\n<img src=\"myproject.Images.test.jpg\">\n</body>\n</html>"
};
MyWebView.Source = htmlSource;
}
Can i load image from path where is inside my app in WebView ? Another method for pinch and zoom ?
Thanks.

You can in fact load content that is bundled into you app with the Xamarin.Forms WebView. See this link: https://developer.xamarin.com/guides/cross-platform/xamarin-forms/user-interface/webview/
I hope that helps!

Related

iOS: embeded Youtube video in Webview inside UITableViewCell not playing

When my WebView is inside a normal UIView every thing works and video plays as expected. (I created a new sample project to test it.)
But when I put the WebView inside a UITableView. Its not playing. It loads the video player with video's thumbnail and big red play button. When I click play button, video won't play. The WebView goes black.
Here's my code
void LoadVideo (string videoId)
{
nfloat width = UIScreen.MainScreen.Bounds.Size.Width - 32;
nfloat height = VideoWebview.Frame.Size.Height;
string embedHtml = "<iframe width=\"{0}\" height=\"{1}\" src=\"https://www.youtube.com/embed/{2}?modestbranding=1&showinfo=0\" frameborder=\"0\" style=\"margin:-8px;padding:0;\" allowfullscreen></iframe>";
string html = string.Format (embedHtml, width, height, videoId);
Console.WriteLine (html);
VideoWebview.ScrollView.Bounces = false;
VideoWebview.LoadHtmlString (html, new Foundation.NSUrl ("https://www.youtube.com"));
}
This works fine when the WebView is not inside a UITableViewCell. What am I doing wrong here? Any help is appreciated.
Edit:
I've found that it's some issue with my project. I created a new sample project and its working inside a UITableViewCell. But in the current project that I'm working it just don't work... not in UITableViewCell, not in UIView.

Webview vertical scrollbar cuts off the content

I am using Webview in my UWP app to render some html content. The problem is, when the content is long enough to produce a scrollbar, the scrollbar would hide some of the content behind itself.
How can I fix it?
Try this code ,that's how i fixed it.
this.webView.InvokeScriptAsync("eval", new string[] { SetScrollbarScript });
string SetScrollbarScript = #"
function setScrollbar()
{
//document.body.style.overflow = 'hidden';
document.body.style.msOverflowStyle='scrollbar';
}
setScrollbar();";`

Resize WebView defined in Xamarin.Forms Core library on screen rotate event

I have web page defined in the core library as view to fill whole screen:
Content = new WebView
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Source = "https://google.com",
};
Unfortunately on screen rotate it's not resized (please see below):
How can I make sure that screen is resized on screen rotation.
If it's iOS, then make a custom WebView renderer (http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/):
protected override void OnElementChanged (VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
ScalesPageToFit = true;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
NativeView.Bounds = UIKit.UIScreen.MainScreen.Bounds;
}
Please try this solution, it works in iOS 8.4, but not iOS 7.
Xamarin Forms: How to resize the Webview after rotation in iOS
You need to be working with webview using Xamarin.forms.
Working with WebView in Xamarin.Forms
xamarin-forms-samples/WorkingWithWebview Has examples for each platform.
This link on Handling Rotation gives you all the details you need for android. The other answer has covered iOS.
Responding To Orientation Changes In Xamarin Forms another great link
Customizing Controls for Each Platform using these premise you can manage the screen rotation for each platform separately within the one app.
Handling Runtime Changes shows how to manage screen rotation. You need to scroll down the page.
A good link on managing windows phone, The two ways to handle orientation in your Windows 8.1 app.
Another useful link Fix IE 10 on Windows Phone 8 Viewport.
This is an interesting link on Managing screen orientation from mozilla. It is experimental, but interesting.
I implemented the custom renderer and added the line "webView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;" but it doesn't work for me. Then, I solved this with a not so elegant solution (reloading the WebView component when the device orientation changes).
In my xaml:
<StackLayout Padding="0" >
<WebView x:Name="viewJupyter" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
</StackLayout>
And my code behind class:
public partial class ServiceChart : ContentPage
{
private int _rotateView;
public ServiceChart(string title, string url)
{
Title = title;
InitializeComponent();
var urlPage = new UrlWebViewSource
{
Url = url
};
viewJupyter.Source = urlPage;
if (Device.OS == TargetPlatform.iOS)
{
SizeChanged += (sender, arg) =>
{
if (_rotateView != 0)
{
var lalala = viewJupyter;
InitializeComponent();
viewJupyter.Source = (lalala.Source as UrlWebViewSource).Url;
}
_rotateView++;
};
}
}
}

Load image on UIWebView and scale it correctly

I load an local image (tif) on to the UIWebView:
string fileName = "image.tif";
string localDocUrl = Path.Combine (NSBundle.MainBundle.BundlePath, fileName);
webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
webView.ScalesPageToFit = true;
The image is bigger than the UIWebView.
Now I want that the image fits in the web view. How can I do this?
i am unable to give comment due to too less comment as i am a new user.
Plz goto nib file and check "Sceling" as "Scale page to fit"
Hope it will work
I'm not sure if this applies directly to a picture, but I had issues trying to get a webview to show correctly on the screen. This is the code that I used and hopefully it can work for you.
var webview = new UIWebView(this.View.Frame);
webview.LoadRequest(new NSUrlRequest(new NSUrl(myCustomUrl)));
this.NavigationController.PushViewController(
new UIViewController() { webview }, true);
Maybe setting the frame will help?

How to fit / displayed the remote image URL into webview Appcelerator Titanium?

I am trying to display a remote image url into webview in both iPhone & Android using Appcelerator Titanium. The remote image is displayed in webview but the full image doesn't fit into the webview in app.
Code:
var webView = Ti.UI.createWebView({
url : 'https://cloudinary-a.akamaihd.net/dhl5ctlq1/image/upload/v1364796700/business_logo_4_53_1364308382.jpg',
top : 0,
left : x(60),
height : '80dp',
width : '80dp',
scalesPageToFit : true,
showScrollbars : false,
scrollsToTop:false,
backgroundColor : "red"
});
Dont use a WebView, a WebView is for displaying web content (read: HTML), instead just use a ImageView, these support remote URL's, just remember that on iOS these remote images are cached, but on Android they are NOT (big oversight on appcelerator's side). Try this instead:
var image = Ti.UI.createImageView({
image : 'https://cloudinary-a.akamaihd.net/dhl5ctlq1/image/upload/v1364796700/business_logo_4_53_1364308382.jpg',
top : 0,
left : x(60),
height : '80dp',
width : '80dp'
backgroundColor : "red"
});

Resources