Windows 8 phone, multi tab browser not updating current url - c#

I am in the process of making a multi tab browser and have run into some problems. I would like it so when the user has added a new tab using a button, that every site they then choose to navigate to will update to the tab url. So if the user changes tab or opens a new one, it the last website will be saved if they return to the previous tab.
Here is the code while creating a new tab & When a tab is selected from the created ones
private void addNewTab(string url)
{
TabEntry urlObj = new TabEntry();
urlObj.URL = url;
urlObj.timestamp = DateTime.Now.ToString("HH:mm");
if (url.Contains("/"))
{
urlObj.Name = url.Remove(url.IndexOf('/'));
}
else
{
urlObj.Name = url.Remove(url.IndexOf('.'));
}
tabs.Insert(0, urlObj);
listBoxTabPage.ItemsSource = null;
listBoxTabPage.ItemsSource = tabs;
Browser.Navigate(new Uri("http://www.google.com", UriKind.Absolute));
//selectedTab = listBoxTabPage.SelectedValue as TabEntry;
}
private void ListBoxTabPage_SelectionChanged(object sender, GestureEventArgs e)
{
selectedTab = listBoxTabPage.SelectedValue as TabEntry;
Browser.Navigate(new Uri("http://www." + selectedTab.URL, UriKind.Absolute));
PivotItems.SelectedItem = BrowserPage;
}
Here is the code where it should update the selected tabs url, in the Browser_Navigated methord
void Browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
_deactivatedURL = e.Uri;
_progressIndicator.IsVisible = false;
string url = Convert.ToString(e.Uri);
//selectedTab.URL = url;
addHistoryRecord(url);
}
Where it is commented out I think is the problem. I believe the code does not know which one is the selectedTab. To fix this error should I create a methord which updates the url of the tab, each time the browser navigates. And how would the program know which tab is currently in use.
If you need any more details please comment and I will be happy to explain in further detail.

You just set listBoxTabPage.ItemsSource = null before you set tabs as ItemsSource. It may clears your listBoxTabPage.SelectedValue.
I think you can just set listBoxTabPage.SelectedValue = urlObj in addNewTab().
Then you begin navigate to a new uri in ListBoxTabPage_SelectionChanged, but it seems to be wrong that you just append a prefix string http://www. to an full url. It may triggers NavigateFailed event if you navigate to a non-exist website.
Browser.Navigate(new Uri("http://www." + selectedTab.URL, UriKind.Absolute));
What's more, ListBoxTabPage_SelectionChanged may triggers twice because the old value is cleared and the new value is set.

Related

XAML: Is it possible to display the current URL in a WebView?

there is a webview in my UWP app. By default the webview does not show the currently loaded contents url. Is it somehow possible to display the url as in the browser search field for instance?
For example if google is loaded in the webview i want to see the url "https://www.google.de/?gws_rd=ssl" somewhere.
Best wishes
You could show the url in a textbox in the NavigationStarting event
void webView1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
string url = "";
try { url = args.Uri.ToString(); }
finally
{
address.Text = url;
appendLog(String.Format("Starting navigation to: \"{0}\".\n", url));
pageIsLoading = true;
}
}
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.navigationstarting.aspx

Making a tabbed cef sharp browser rename tab page

i have been creating a cefsharp browser in C#. i have made it so you can have multiple tabs and it loads the pages correctly. however, i cannot seem to find how i can rename the tab to the name of the page.
this is the code in the load event for form1.cs:
Cef.Initialize();
toolTip1.SetToolTip(button1, "Settings");
TabPage tab = new TabPage();
Tab newtab = new Tab();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
tab.Controls.Add(newtab);
tabControl1.TabPages.Add(tab);
i have tried:
private void myBrowser_isLoading(object sender)
{
myBrowser.Parent.Parent.Text = myBrowser.Title;
}
but that doesn't work.
then this is the code for tab.cs:
public Tab()
{
InitializeComponent();
// Start the browser after initialize global component
InitializeChromium();
}
public CefSharp.WinForms.ChromiumWebBrowser myBrowser;
public bool nav = new bool();
public void InitializeChromium()
{
myBrowser = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com");
this.Controls.Add(myBrowser);
myBrowser.Dock = DockStyle.Fill;
myBrowser.Parent = panel2;
if (nav == true)
{
myBrowser.Load(textBox1.Text);
nav = false;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
again, i am using c# with the latest build of cef sharp(or atleast the one installed from the nuget package manager).
In your tab function in form1.cs, you need to add a title changed function like this
browser.TitleChanged += OnBrowserTitleChanged;
you also need to specify what browser is and set dockstyle to fill like this
ChromiumWebBrowser browser = new ChromiumWebBrowser("google.com");
tab.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
now for the OnBrowserTitleChanged, you will need an EventArg which will tell the tab to have the document title in this format
this.InvokeOnUiThreadIfRequired(() => browserTabControl.SelectedTab.Text = args.Title);
this will add the document title to the tabcontrol browserTabControl is the name of the tabcontrol you will have to change browserTabControl to whatever name you have for the tabcontrol. Also the code you have does not belong with cef initialize. you need to create an addNewTab method with all the functions that will be processed when you want to add a new tab. Also, you cannot use a panel if you want to have tabs. you need a tabcontrol

How to show a hyperlink inside the description field of an AboutBox

I want to place a link inside the description field of my applications about box which will direct users to a wiki page for more help. I can't figure out how to make the address appear as a link.
I set the description through the assembly information properties.
There's a WinForms control you can use to achieve what you want: the LinkLabel.
Simply add one to your AboutBox layout and double click it. A handler to its LinkClicked event will be created, and there you can then use Process.Start to open your website's URL.
public AboutBox1()
{
InitializeComponent();
this.Text = String.Format("About {0}", AssemblyTitle);
this.labelProductName.Text = AssemblyProduct;
this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
this.labelCopyright.Text = AssemblyCopyright;
this.labelCompanyName.Text = AssemblyCompany;
this.textBoxDescription.Text = AssemblyDescription;
this.Link.Text = "Visit our website!";
this.Link.Tag = WpfApplication2.Properties.Resources.website;
}
private void Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start((sender as LinkLabel).Tag.ToString());
}
In my case, I've saved the URL as an application resource. And I've showed it separately from the Assembly Description.
If you want the link to appear inside the Assembly Description, it's quite a bit more complicated...

Prevent multiple instances of a page in TabControl

I am making an application where i am opening wpf pages in tab control. But i am able to open same page again and again in tabcontrol. I want that if once page is opened it can't be opened again and it should get focused on tabcontrol if i try to open it again. i did following code but not working. I am using a custom closableTabItem Usercontrol.
private void Set_Fee_Click(object sender, RoutedEventArgs e)
{
// Adding page to frame and then adding that frame to tab item and then adding tab item to main tab.
FeeStructure feePage = new FeeStructure();
_closableTab = new ClosableTabItem();
_formFrame = new Frame();
_formFrame.Content = feePage;
_closableTab.Content = _formFrame;
_closableTab.Header = "Set Fee Structure";
if (!mainTab.Items.Contains(_closableTab))
{
mainTab.Items.Add(_closableTab);
_closableTab.Focus();
}
else
{
_closableTab.Focus();
}
}
private void Database_RecoveryBackup_Click(object sender, RoutedEventArgs e)
{
// Adding page to frame and then adding that frame to tab item and then adding tab item to main tab.
DbRecoveryBackup dbRecBack = new DbRecoveryBackup();
_closableTab = new ClosableTabItem();
_formFrame = new Frame();
_formFrame.Content = dbRecBack;
_closableTab.Content = _formFrame;
_closableTab.Header = "Data Base";
if (!mainTab.Items.Contains(_closableTab))
{
mainTab.Items.Add(_closableTab);
_closableTab.Focus();
}
else
{
_closableTab.Focus();
}
}
It'll never happen, what you want because you're creating a new instance of ClosableTabItem everytime, hence it is unique everytime, so .Items.Contains will never work in this case because it matches items using object.Equals.
Now, Since you said in question that you only want one instance of ClosableTabItem, then
using Linq, you can check if in the items there exist any item of type ClosableTabItem,
...
// Here we're checking the array 'Items',
// if it contains any item whose type is 'ClosableTabItem'
if (!mainTab.Items.Any(item => item is ClosableTabItem)))
...

Windows Phone Emulator Functions Properly, Debugging or Deploying to Device Does Not

I am developing a very simple application that parses an XML feed, does some formatting and then displays it in a TextBlock. I've added a hyperLink (called "More..) to the bottom of the page (ideally this would be added to the end of the TextBlock after the XML has been parsed) to add more content by changing the URL of the XML feed to the next page.
The issue I'm experiencing is an odd one as the program works perfectly when in the Windows Phone 7 Emulator, but when I deploy it to the device or debug on the device, it works for the first click of the "More..." button, but the ones after the first click just seem to add empty space into the application when deployed or debugged from the device.
I'm using a Samsung Focus (NoDo) and originally thought this may have had to do with the fact that I may not have had the latest developer tools. I've made sure that I am running the latest version of Visual Studio and am still running into the issue.
Here are some snippets of my code to help out.
I've declared the clickCount variable here:
public partial class MainPage : PhoneApplicationPage
//set clickCount to 2 for second page
int clickCount = 2;
Here is the snippet of code I use to parse the XML file:
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
ListBoxItem areaItem = null;
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
string areaName = String.Empty;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "example")
{
areaName = reader.ReadElementContentAsString();
areaItem = new ListBoxItem();
areaItem.Content = areaName;
textBlock1.Inlines.Add(areaName);
textBlock1.Inlines.Add(new LineBreak());
}
}
}
}
}
and the code for when the hyperLink button is clicked:
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
int stringNum = clickCount;
//URL is being incremented each time hyperlink is clicked
string baseURL = "http://startofURL" + stringNum + ".xml";
Uri url = new Uri(baseURL, UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
//increment page number
clickCount = clickCount + 1;
}
It feels like there's a little more debugging to do here.
Can you test where exactly this is going wrong?
is it the click that is not working on subsequent attempts?
is it the HTTP load which is failing?
is it the adding of inline text which is failing?
Looking at it, I suspect it's the last thing. Can you check that your TextBlock is expecting Multiline text? Also, given what you've written (where you don't really seem to be making use of the Inline's from the code snippet I've seen), it might be easier to append add the new content to a ListBox or a StackPanel rather than to the inside of the TextBlock - ListBox's especially have some benefit in terms of Virtualizing the display of their content.

Categories

Resources