Loading a local html in WebBrowser - windows-phone-7

I am trying to load a local html file into a WebBrowser control. However, the resources that come with that html are added to the project but not loaded in the html. I copied them in the same directory, marked them as "copy-always" resources.
Since I am new to windows phone development, any help will be greatly appreciated. Thanks.

Mark the build action for file as 'Content' from 'Properties' and then try the following :
(Assuming you have b.html file in 'html' folder of your project.)
var rs = Application.GetResourceStream(new Uri("html/b.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowserCtrl.NavigateToString(htmlText) ;

(Assuming you have Help.html file in 'Help' folder of your project.)
if you're Mark the build action of the file as 'Content'
var rs = Application.GetResourceStream(new Uri("/Help/Help.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowser1.NavigateToString(htmlText);
if you're Mark the build action of the file as 'Resource'
var rs = Application.GetResourceStream(new Uri("/YOUR_PROJECT_NAME;component/Help/Help.html", UriKind.Relative));
StreamReader sr = new StreamReader(rs.Stream);
string htmlText = sr.ReadToEnd();
sr.Close();
webBrowser1.NavigateToString(htmlText);

You should mark the HTML File as Content. It should be "Resource" right now.

Related

Open PDF file with page number/index in UWP apps

Is there any option to Open a PDF file that is available in local state folder (inside app installation directory) with the page number. There is one method (launchFileAsync) to open such files but I don't find any option to pass page number/index to open specific page.
Thank you! Any help is appreciated.
UWP provides the PdfDocument class for parsing PDF files, and this class provides the GetPage method for obtaining the object of the corresponding page (PdfPage) through the index.
This is simple code:
PdfDocument pdfDocument;
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("xxx.pdf");
pdfDocument = await PdfDocument.LoadFromFileAsync(file);
using (var firstPage = pdfDocument.GetPage(0))
{
var stream = new InMemoryRandomAccessStream();
await firstPage.RenderToStreamAsync(stream);
// do something...
}
Here is the complete code example:
PDF document sample

webview load html form local Download folder working for IOS not Working for Android in Xamarin forms

We have a html Project inside a folder . Html Project contains .htm,.js ,css and image resources .
(Like Download -> ProjectName-> Project Folder (htm,css,js,icons)). Its store in download folder .
We want webview load index.htm form project Folder .
in Ios we return the absolute path of project folder and combine path with index.htm its
Load successfully in Ios .
webview.source= new UrlWebViewSource(Url=urlPath);
but Same Implementation not working for Android and I tried with StreamReader approche it loads only index .html not redirect to other page also ...*
*Code For Xamarin.Android
string MyfileURL= "Created path to download " ;
bool shouldShow = await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage);
string RootPath = DependencyService.Get().downloadPath("FilePAth","ContainerLink");
var source = new HtmlWebViewSource();
source.Html = RootPath;
webview.Source = source;
AndroidRenderer
var RootPath = Path.Combine(localPath, "t370_naturaline_offline");
var fileFullNamePath = Path.Combine(RootPath, "index.htm");
StreamReader stream = new StreamReader(fileFullNamePath);
string output =stream.ReadToEnd();
return output;//method return output String .*

Adding image on dynamically created Button in Silverlight

I am creating the button in silver light dynamically as
HistoryButtton = new Button();
HistoryButtton.Content = "History";
HistoryButtton.Height = GetPercentageValue(TotalHeight, 8);
HistoryButtton.Width = GetPercentageValue(TotalHeight, 15);
I tried this solution
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(#"Images/history.png", UriKind.Relative));
HistoryButtton.Background = brush;
But its not working.
I also tried this solution
Uri uri = new Uri("/Images/history.png", UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uri);
Image image = new Image();
image.Source = imgSource;
HistoryButtton.Content = image;
But this solution also not working. Please help me.Thanks in advance.
try to change your Uri first parameter to be like this
"/SolutionName;component/Images/history.png"
replace SolutionName with your Solution name.
The question and one of the other answers here helped me with the same problem, but I still struggled with this for awhile (actually, more than awhile) because of some really simple things that I missed. Here is the complete code that worked for me:
Dim b As New Button
Dim uri As New Uri("/<project name>;component/<path to image>", UriKind.Relative)
Dim imgSource As New Imaging.BitmapImage(uri)
Dim image As New Image()
image.Source = imgSource
b.Content = image
Two things that kept me from figuring this out sooner:
<project name> is the name of the project where the image resides, which may not be the same project the code resides in.
"component/" is a literal string (and is not a folder in the project). My image file's "Build Action" is set to "Resource".
So if your project name is "MyProject", your image name is "MyImage.png", and the image resides in the folder "MyFolder" in "MyProject", the correct string to pass to the Uri constructor is:
"/MyProject;component/MyFolder/MyImage.png"

webBrowser in windows forms: how to load html page from resources?

I have program which has webBrowser component. I need that this component would navigate to page which is in Resources ("1.htm"). Is there anyway to do it? My general wish is that after debuging I would have only one .exe file of program, and all htm pages would be build in it (like pictures and icons), or isnt that posible?
You can use DocumentText property for that
webBrowser1.DocumentText = WindowsFormsApplication1.Properties.Resources.1htm;
or
using (Stream stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("1.htm"))
{
using (StreamReader reader = new StreamReader(stream))
{
webBrowser1.DocumentText = reader.ReadToEnd();
}
}

Loading an image from resources in an unreferenced assembly

I have two silverlight assemblies, CaseManager.Applet and CaseManager.Applet.Sample. The Sample assembly has a reference to the base Applet assembly. The sample assembly has an embedded resource png image. The base applet assembly has a view xaml that I wish to display that image programmatically.
In the sample assembly I have a bit of code that creates a Uri like so:
var icon = new AppletIcon()
{
ImageUri = new Uri("CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),
ModuleType = GetType(),
Text = "Sample Module"
};
When I execute this code all the properties of ImageUri throw InvalidOperationException. I am not sure why. Anyone have suggestions?
The following code does the job:
var icon = new AppletIcon()
{
ImageUri = new Uri("/CaseManager.Applet.Sample;component/images/guiness_2.png", UriKind.Relative),
Module = this,
Text = "Sample Icon"
};
Things to note here:
The slash at the start of the Uri string.
The short name of the assembly containing the resource.
the ;component/ section.
From there it is basically the path inside your project to the image. Hope this helps someone else.
For what it was worth I was missing the very first slash.

Resources