How to create new tabs in xamarin forms web view - c#

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);

Related

IOS don't show logo in splash screen

I create a SplashPage.cs in my Xamarin App, code:
public class SplashPage : ContentPage
{
Image splashImage;
public SplashPage()
{
NavigationPage.SetHasNavigationBar(this, false);
var sub = new AbsoluteLayout();
splashImage = new Image
{
Source = "logo.png",
WidthRequest = 100,
HeightRequest = 100
};
AbsoluteLayout.SetLayoutFlags(splashImage, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(splashImage, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
sub.Children.Add(splashImage);
this.BackgroundColor = Color.FromHex("#ffff");
this.Content = sub;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await splashImage.ScaleTo(1, 2000);
await splashImage.ScaleTo(0.9, 1500, Easing.Linear);
await splashImage.ScaleTo(150, 1200, Easing.Linear);
Application.Current.MainPage = new NavigationPage(new LoginPage());
}
}
Then in App.xaml.cs
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new SplashPage());
}
Directory in Android is Resources/drawable/logo.png
Directory for IOS is Resources/logo.png
When I compile app in a android phone splash load correctly, but when I try to use in IOS it just no load my icon, only show empty page. I'm doing something wrong?
Yes,this is often the case when you are using images that are not formatted correctly or are very complex.And I've had similar problems before.
Vector graphics are highly recommended in ios.
I have tested this question, when i use a vector image , it worked properly both in ios and android.
Note: You can try it using the following image.
Main purpose of the splash screen is give some time to load application and its components. In case of Xamarin.Forms app, this time is used to load Xamarin runtime.
Now, your splash screen it self is using Xamarin.Forms component ContentPage. So it can only be displayed after Xamarin runtime is completely loaded.
Best way is to implement splash screen the native way for iOS and Android.
Here is a good tutorial about how to implement splash screen natively on iOS and Android.

NotifyTaskCompletion Xamarin UI in C#

I´ve a question regarding INotifyTaskCompletion (https://msdn.microsoft.com/en-us/magazine/dn605875.aspx).
When i use this patter in an UI, created in XAML everything works fine.
But when i want to use it in a UI generated by Code (C#) it doesen´t work.
Viewmodel is the same.
I have have a Test project in this Link.
https://github.com/marcuskammerlander/NotifyTaskCompletion
You can change the UI in App.xaml.cs
public Testpage()
{
//Work
this.BindingContext = new testpageViewModel();
TestCount.SetBinding(Label.TextProperty, new Binding("UrlByteCount.Result", BindingMode.OneWay));
//Doesent Work
//testpageViewModel viewModel = new testpageViewModel();
//this.BindingContext = viewModel;
//TestCount.SetBinding(Label.TextProperty, new Binding(nameof(viewModel.UrlByteCount.Result), BindingMode.OneWay));
Content = new StackLayout
{
Children = {
TestLabel, TestCount
}
};
}
Regards,
Marcus

How do you load a url in a UIWebView

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;

How to open a dialog center-screen or center-owner?

I have a MVVM application and I have a main viewmodel. In this model I use the following code to open a dialog:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = myDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.WindowStartupLocation = WindowStartupLocation.CenterOwner;
myDlgView.ShowDialog();
Also, in the XAML on my dlgView, I set the WIndowsStartupLocation to CenterOwner. However the window does not open in the center of the owner. I tried CenterScreeen but that does not work either.
How can I open a new view using the centerOwner or centerScreen option?
Well, the first time thing is set in the axml of my dialog WindowsStartUpLocation to center owner.
Then, in the main view model, I use this code:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = miDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.ShowDialog();

How can I add this WPF control into my WinForm?

I know that I must use an ElementHost to display a WPF control in a WinForm, but as the WPF control is third party software and it only comes with an XML file and a DLL file.
The control is AvalonEdit, I added both the ICSharpCode.AvalonEdit.xml and ICSharpCode.AvalonEdit.dll files to my project, and I went to Project -> Add Reference and added the DLL as a reference. Now I can access the ICSharpCode namespace in my code, all of the classes and methods are exposed, but from this point I am unsure how to actually use the control in my WinForm.
I was expecting a WPF control to appear in the Solution Explorer, but it does not. I tried adding an ElementHost control to my WinForm anyways, but when I try to Select the Hosted Content, no controls appear, so it doesn't know about my WPF control. How can I use the AvalonEdit WPF control in my WinForm?
If you want to be able to set the hosted content at design time the control needs to be part of your solution. One way to achieve that is to create a custom WPF user control which contains the AvalonEdit component you want to use. I.e
Create a WPF User Control library project and create a user control
containing the AvalonEdit component.
Add the User control project to your Winforms solution.
Now you should be able to select your new user control as the hosted content.
Or you could add the AvalonEdit control directly in code like this:
public Form1()
{
InitializeComponent();
ElementHost host= new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100,100);
AvalonEditControl edit = new AvalonEditControl();
host.Child = edit;
this.Controls.Add(host);
}
Not sure what the control is called so replace the AvalonEditControl as appropriate.
You'll want an example on how to do Code Colouring/Syntax Highlighting as well:
public Form1()
{
InitializeComponent();
ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
textEditor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
textEditor.FontSize = 12.75f;
string dir = #"C:\Temp\";
#if DEBUG
dir = #"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif
if (File.Exists(dir + "CSharp-Mode.xshd"))
{
Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
// Apply the new syntax highlighting definition.
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();
}
//Host the WPF AvalonEdiot control in a Winform ElementHost control
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
host.Child = textEditor;
this.Controls.Add(host);
}
ElementHost host = new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100, 100);
ICSharpCode.AvalonEdit.TextEditor edit = new
ICSharpCode.AvalonEdit.TextEditor();
host.Child = edit;
this.Controls.Add(host);

Categories