Windows8 App how to use Iframe - xaml

I am trying load a html file inside my xaml file.I tried this
<WebView HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Name="WebView1" Margin="109,82,0,0"/>
string url = "ms-appdata:///local/assets/index.html";
WebView1.Navigate(new Uri(url));
But is not working.I would like to know is this possible or is there any other way.
Thanks

You can't point the Uri to a file in app data folder, try something like this instead:
using Windows.Storage;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("assets/index.html");
string html = await FileIO.ReadTextAsync(file);
WebView1.NavigateToString(html);

Related

Xamarin Forms image binding on url with default on embedded resource

i have a Xamarin ListView with images from internet. I would to show a default image (from embedded resource), and, when present, the image from an url.
I've tryied with this code but works only with UriImageSource, not for the embedded resource:
<ListView ItemsSource="{Binding Data}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Image>
<Image.Source>
<!-- does not works
<ImageSource>
{utils:ImageResource Namespace.Assets.img-placeholder.png}
</ImageSource>
-->
<UriImageSource Uri="{Binding ImageUri}"></UriImageSource>
</Image.Source>
</Image>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
if i use
<Image Source="{utils:ImageResource Namespace.Assets.img-placeholder.png}" />
this works showing the embedded resource default image
The model:
public Uri ImageUri => Image != null ?
new Uri($"http://www.example.com/{Image}")
: null;
Any help? Thanks
You can use FFImageLoading library.
According to its documentation:
LoadingPlaceholder
ImageSource property. If set, a loading placeholder is shown while
loading. It supports UriImageSource, FileImageSource and
StreamImageSource.
ErrorPlaceholder
ImageSource property. If set, if error occurs while loading image, an
error placeholder is shown. It supports UriImageSource,
FileImageSource and StreamImageSource.
You don't have to make it that complicated. From your viewmodel you simply provide a string ImageUri and bind it to Image.Source
<Image Source="{Binding ImageUri}" />
this works for images from the web as well as resources, but they have to live in your platform projects. For Android you'll have to put them to Resources\drawable (or the respective folders for other resolutions) and set the build action to AndroidResource, for iOS to Resources (in different sizes with the #2x, #3x suffices, e.g. img-placeholder#2x.png) and set the build action to bundle resource. You can now set the placeholder from your viewmodel:
if(!HasRemoteImage(response))
{
ImageUri = "img-placeholder.png";
}
Xamarin.Forms will create the ImageSource automatically for you.
One main advantage over resources you load directly is, that you don't have to care which image is the right one for the current resolution. Xamarin/Xamarin.Forms does that automatically for you.

Load data with base uri Windows Phone

Is in windows phone equivalent of IOS method called loadHTMLString:baseURL or android loadDatawithBaseUrl (here). I want to run script on website and that is why I need this base url to be invoked.
By looking at the reference link, I think what you are trying to achieve is a web browser control on Windows Phone to display a remote HTML page on the app page.
LOAD FROM REMOTE URL:
Step 1: Load WebBrowser control on your app page from the Toolbox on page.xaml
<Grid x:Name="ContentGrid" Grid.Row="1">
<phone:WebBrowser HorizontalAlignment="Left" Margin="0,0,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="800" Width="470" />
</Grid>
Step 2: Write C# code (you can even use VB.NET) on page.xaml.cs
webBrowser1.Source = new Uri("http://www.foo.com", UriKind.Absolute);
LOAD FROM LOCAL HTML STRING
Here Step 1 remains the same, just to load an HTML code locally:
Step 2: C# Code on page.xaml.cs
String htmlContent = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">
<meta name=\"spanish press\" content=\"spain, spanish newspaper, news,economy,politics,sports\">
<title></title>
</head>
<body id=\"body\">
<!-- The following code will render a clickable image ad in the page -->
<script src=\"http://www.myscript.com/a\"></script>
</body>
</html>";
webBrowser1.NavigateToString(htmlContent); //This renders the HTML string defined above in the webview.
EDIT: Also, if you want to allow scripts to execute in the webview, set IsScriptEnabled property of the webbrowser control to true.
webBrowser1.IsScriptEnabled = true;

XAML MediaElement Play() method not working in code

Hi I have a Windows Phone app with a MediaElement window defined in XAML to play a video file:
<MediaElement x:Name="mediaWindow" MediaEnded="mediaWindow_MediaEnded" Stretch="UniformToFill" HorizontalAlignment="Left" Height="225" Margin="15,127,0,0" VerticalAlignment="Top" Width="450" AutoPlay="True"/>
When I call this in C# code it play fine:?
mediaWindow.Source = new Uri(loc, UriKind.Relative);
But something strange is happening eg when the user navigates to a new page a MediaElement on that page should kickin and play an audio file but I doesn't. If I comment out the code above which play the video then the audio file on the navigated to page works OK.
I've tried using in XAML:?
<MediaElement x:Name="mediaWindow" MediaEnded="mediaWindow_MediaEnded" Stretch="UniformToFill" HorizontalAlignment="Left" Height="225" Margin="15,127,0,0" VerticalAlignment="Top" Width="450" AutoPlay="False"/>
and in C#:
mediaWindow.Source = new Uri(loc, UriKind.Relative);
mediaWindow.Play();
But this just shows a black screen!?
These is another MediaElement defined in XAML on the original page which I use to play an audio file, could this be the problem? If so what can I do to play the audio file and a video file on the same page? I've read I could use MediaPlayer for the audio file but I tried defining that in c# code but it does not exixt in namespace even though I have included:
using System.Windows.Media;
This is driving me crazy, new to C#, help appreciated here.
Many thanks
Did you try giving the source of the video within the XAML? and make the AutoPlay to false.
<MediaElement x:Name="mediaSimple"
Source="Videos/video1.mp4"
Stretch="UniformToFill" AutoPlay="False"/>
and give the mediaSimple.Play() within a button click event handler.

How to localize page title using resource files (.resw)?

I need to translate the title of basic page in windows8 app. Here is a xaml string:
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<x:String x:Uid="title_select_profile" x:Key="AppName">Select profile</x:String>
</Page.Resources>
So, the trick with Uid doesn't work here. How to get accsess to this string from resource file?

Download with Webview Windows 8

I'm really stuck with an issue. I would like to download a PDF/ZIP formatted file from webview but I can't find any solution/tutorial on the internet, I tried to look webview property class.
i have a webview:
WebView x:Name="webviewIntranet" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
and my method:
public IntranetPage()
{
webviewIntranet.Navigate(new Uri("http://www.testme.com"));
this.InitializeComponent();
}
I'm able to show the webpage content but nothing happens when a try to download/click to download some files.
First of all some reading about what's new in WebView control
http://blogs.windows.com/windows/b/appbuilder/archive/2013/07/17/what-s-new-in-webview-in-windows-8-1.aspx
as you can see, WebView hanldes different browser events:
It doesn't support showing nothing else than webpages, but WebView.UnviewableContentIdentified event is fired for them
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.unviewablecontentidentified.aspx
Then you can use file association to throw it to the OS (or do what you need with the file throught the uri... i hope)
void webView1_UnviewableContentIdentified(WebView sender,
WebViewUnviewableContentIdentifiedEventArgs args)
{
Windows.Foundation.IAsyncOperation<bool> b =
Windows.System.Launcher.LaunchUriAsync(args.Uri);
}
WebView can't download file. Microsoft has tried not to be WebView, a replacement of IE browser. See the answer from MSDN forum.
I think you can do this, but there's quite a bit of manual intervention. Using something like the BackgroundDownloader you can download a file that you have the URI for. Is you want to also intercept web view navigation, then there's a few workarounds for that; for example: here