How do you load a url in a UIWebView - c#

I just started creating a simple application in Xamarin C#.
I have two views : first view have a "login" button and a second view have a webView inside.
I have created the modal segue which connect those two views and my question is :
How can I get if second view is showing it's content and it's webView can load this url : "http://vk.com" ?

Well, at first, you need to add two views.
On the first view place a button. Connect this button to a second view(create a modal segue)
On the second view you can place a label or something different, it doesn't matter.
Zoom out your storyboard(in Xcode storyboard editor, "-" icon), select and set the second view class, for example, "view_two". Save and return to Xamarin Studio. Restart Xcode. Now, in Xamarin Studio select Your class file(in this case - view_two.cs) and open it. Then, insert this code in the "center" of the document :
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.webView_login.LoadRequest (new NSUrlRequest (new NSUrl ("http://vk.com")));
}

In the View's ViewDidLoad method:
string url = "http://vk.com";
webView.LoadRequest(new NSUrlRequest(new NSUrl(url)));
See also: http://docs.xamarin.com/recipes/ios/content_controls/web_view/load_a_web_page/

This below piece will will help.
UIWebView webView = new UIWebView(View.Bounds);
View.AddSubview(webView);
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var yourWonFolderPath = Path.Combine(documents, "YourWonFolder");
var localDocUrl = Path.Combine(yourWonFolderPath, TicketInfo.TicketNumber + ".pdf");
webView.LoadRequest(new NSUrlRequest(new NSUrl(localDocUrl, false)));
webView.ScalesPageToFit = true;

Related

PopAsync not working when i use it in a TabbedPage ViewModels

Ok so in my previous design, on my MainPage which is called TasksGroupPage, i had a "+" icon that was taking me to a NewTasksGroupPage to fill a form and then click the save command to create a new TasksGroup ( so i don't need the + icon function to load the page anymore).
This was working perfectly and after clicking the save button it would take me to the MainPage.
But my problem is that i changed my design, i now removed the + icon and instead i created a new tab which now takes me to the NewTasksGroupPage.
To create this tab i simply added to my TasksGroupPage( the main page) the following code : <views:NewTasksGroupPage></views:NewTasksGroupPage>
The problem now is that :
When i click the save command in my NewTaskPageViewModel , the page doesn't take me to the last page like it did before ALSO it's leaving all the text in the entry box there and it only create my TasksGroupPage after a while.
How should i set up this new design to work properly ? If impossible to do, is there a way to add a GestureRecognizers to one of the tab ?
Thanks.
Here you can see my code right now :
TasksGroupPage.xaml
<TabbedPage>
<ContentPage>
// some xaml
</contentPage>
<views:NewTasksGroupPage></views:NewTasksGroupPage>
</TabbedPage>
TasksGroupPage.xaml.cs this is the function that i don't need anymore but this is what i was using with the + icon
protected async void GoToNewTaskPage(object sender, EventArgs e)
{
await Navigation.PushAsync(new Views.NewTasksGroupPage());
}
NewTaskPageViewModel.cs this is the end of my save command which before took me to the main page ( TasksGroupPage) but now it just stays there and doesn't reset my entries to blank ( even when switching between other tabbed pages)
async Task SaveNewTask()
{
//some code
await Application.Current.MainPage.DisplayAlert("Save", "La tâche a été enregistrée", "OK");
await Application.Current.MainPage.Navigation.PopAsync();
NotifyPropertyChanged();
}
If you read the document Xamarin.Forms Navigation:
You will find that NavigationPage and TabbedPage are two different ways to navigate pages in Xamarin.forms.
Navigation.PushAsync and Navigation.PopAsync only works in NavigationPage, it does not work in TabbedPage, that's why the page doesn't take me to the last page like it did before.
If you use a TabbedPage, you can use below code to switch page:
async void SaveNewTask(object sender, EventArgs args)
{
//this here means the current ContentPage
//this.Parent = NavigationPage
var tabbedPage = this.Parent.Parent as TabbedPage;
//if you don't have navigationPage, just use:
//var tabbedPage = this.Parent as TabbedPage;
//Switch to first page in tabs
tabbedPage.CurrentPage = tabbedPage.Children[0];
}

How to create new tabs in xamarin forms web view

I want to make my web-view support multiple tabs .
how to do it for example like this code :
browser.newtab(URL:"google.com",tabindex:1);
How to do it in android and ios ?
That is not possible. WebView (and its native counterparts) don't support the tab browsing. So there is not a single line of code that will resolve your problem You must create the tab UI yourself and manage everything related to it.
Easiest solution is to go for tabbed page. Example:
private ContentPage NewBrowserTab(string url)
{
var cp = new ContentPage();
cp.Title = url;
var wv = new WebView();
wv.Source = url;
cp.Content = wv;
return cp;
}
And tabbed page:
TabbedPage tp = new TabbedPage();
tp.Children.Add(NewBrowserTab("https://google.com"));
tp.Children.Add(NewBrowserTab("https://xamarin.com"));
App.Current.MainPage = new NavigationPage(tp);

Navigate between nested View Controllers across a TabBarController - Xamarin iOS

I'm having trouble with navigation outside of the typical use cases for navigation controllers and tabbar controllers. Here's a drawing of a simplified version. (real version has a TabBarController that feeds into 9 NavControllers)
In my Home Nav Controller, there's a table view where the user could select a row and that should navigate them to a particular Events Detail View Controller. At that point, the user should be able to navigate back to the Events TableView Controller to see the list of events or use the TabBar to navigate to another section.
So far with my code I can push the correct detail view controller and select the correct index of the tabbar but it only works the first time:
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
var eventsDetailView = new EventsDetailController(eventPosts[indexPath.Row]);
//loop through ViewControllers array in case the user has set a custom tabbar order via the More Tab
foreach (UIViewController viewC in tbController.ViewControllers)
{
UINavigationController navController = viewC as UINavigationController;
if (navController.TabBarItem.Tag.Equals(9))
{
navController.PushViewController(eventsDetailView, false);
tbController.SelectedIndex = 9;
}
}
}
After the user has navigated to the events detail view controller, if they then:
travel to the Home ViewController via the TabBar
select a new TableRow to navigate to a different Events detail view controller
... then the same previous events detail view shows up. It doesn't matter which row the user selects in the Home View Controller. The initial detail view controller will always be the same one to be presented.
Any help pointing me in the right direction would be greatly appreciated!
Solution:
In my understanding, what you want is :
HomePageController --> EventDetailPage --> EvevtTableViewControll
So in the first step, you could push to EventDetailPage in HomePageController:
this.NavigationController.PushViewController(new HomeDetailUIViewController(), true);
Then, in your EventDetailPage, you can customize a back button, for example:
UIBarButtonItem backBtn = new UIBarButtonItem();
backBtn.Title = "back";
backBtn.Style = UIBarButtonItemStyle.Plain;
this.NavigationItem.LeftBarButtonItem = backBtn;
backBtn.Clicked += (sender, e) =>
{
// 1 here is an example, it should be the index of your EvevtTableViewControll
NavigationController.TabBarController.SelectedIndex = 1;
NavigationController.PopToRootViewController(true);
};
Set the NavigationController.TabBarController.SelectedIndex=1 first to make sure EvevtTableViewControll will show after you back from EvevtDetailViewControll, then your PopToRootViewController to back to the EvevtTableViewControll.
Try it and let me know if you have any problem.

onCreateView not called on fragment that was inflated by layoutinflator

I have a layout that I want to show as a popup window (used as a custom options menu) while in a fragment of a viewpager. Therefore, when the "Options" button is clicked, I do the following:
public void onOptionsButtonClicked(int buttonHeight)
{
LayoutInflater optionsLayoutInflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
int popupWidth = ViewGroup.LayoutParams.MatchParent;
int popupHeight = ViewGroup.LayoutParams.WrapContent;
View layout = optionsLayoutInflater.Inflate(Resource.Layout.TrackOptions, null);
int popupYOffset = (85 + buttonHeight) * -1;
var popup = new PopupWindow(context);
popup.Focusable = true;
popup.Width = popupWidth;
popup.Height = popupHeight;
popup.ContentView = layout;
popup.SetBackgroundDrawable(new BitmapDrawable());
popup.OutsideTouchable = true;
popup.ShowAsDropDown(view, 0, popupYOffset);
}
And this works as I want, visually that is. Meaning, I click the button and I do see the layout popup as a popup window with all of my options. HOWEVER, none of the buttons work. I put a breakpoint in the class that should be associated the the layout and noticed that onCreateView never gets called, therefore, none of the buttons and associated click event handlers are ever wired up. So, I know why it is not working. However, I don't know how to fix it. I think it is because, while I inflate the view, I am never actually creating the fragment. I have done fragementmanager transactions to replace a fragment in other parts of my project and I know that would probably do it, however, this is a different case as I am trying to do a popup window.
Thanks!
Mike
Fragment is attach in activity so you can try it in onActivityCreated(Bundle) method

Xamarin IOS navigate to another view controller without button on xamarin

i start using xamarin to create ios software.
in mainstoryboard, i navigate PageMain view controller to PageTo view controller.
I want navigate from PageMain view controller to PageTo view controller. i use this code and did not auto navigate:
var storyBoard = UIStoryboard.FromName ("MainStoryboard", null);
storyBoard.InstantiateViewController ("PageTo");
tried this one too but also not auto navigate :
PageMainViewController *viewController = segue. PageToViewController;
viewController.delegate = self;
tried this one too but also not auto navigate :
UIViewController pageto = new PageTo ();
pageto.Transition (PageMain, PageTo);
i know it, it easier use button to create push seque to PageTo view controller, but i did not want it.
please help me.
Another thing that you can do is push to another view controller...
AssignCaseController assignCaseController = this.Storyboard.InstantiateViewController("AssignCaseController") as AssignCaseController;
if (assignCaseController != null)
{
assignCaseController.CaseID = GetCurrentCaseID();
this.NavigationController.PushViewController(assignCaseController, true);
}
I hope this helps, I just had to do the same.
// Clear your notifications and other things
// Show the controller
// I am assuming your are using storyboards based in your use of Handle. I am also assuming the identifier in your storyboard for this is set to "WebViewController and its a custom subclass of UIViewController named WebViewController.
UIStoryboard Storyboard = UIStoryboard.FromName ("MainStoryboard", null); // Assume the name of your file is "MainStoryboard.storyboard"
var webController = Storyboard.InstantiateViewController("WebViewController") as WebViewController;
// ... set any data in webController, etc.
// Make it the root controller for example (the first line adds a paramater to it)
webController.CaseID = int.Parse(notification.UserInfo["ID"].ToString());
this.Window.RootViewController = webController;
// Note* the "Window" should already be set and created because the app is running. No need to remake it.

Categories