I have a C# WinRT app that uses the WebView control. None of the events I have created event handlers for are firing:
Uri theUri = new Uri("http://bing.com/");
webViewVideoPlayer.NavigationStarting += webViewVideoPlayer_NavigationStarting;
webViewVideoPlayer.NavigationCompleted += webViewVideoPlayer_NavigationCompleted;
webViewVideoPlayer.ContentLoading += webViewVideoPlayer_ContentLoading;
webViewVideoPlayer.NavigationFailed += webViewVideoPlayer_NavigationFailed;
webBrowserVideoPlayer.Navigate(theUri);
I have set breakpoints on the first line in each of the event handler bodies. The web page renders fine, but none of the breakpoints are hit. What can I do to fix this?
The app is currently configured for Debug build, x86 platform.
[TO THE MODERATORS: This is not my best post obviously. I was tired and made a silly mistake. If you feel it is best to delete it, please do.]
You've got two different control names in your code. The events are attached to webViewVideoPlayer, yet later the Navigate is called on a different WebView called webBrowserVideoPlayer.
I did a test with a WebView and the events were called as expected.
Related
This could seem to be duplicates of other similar questions but they are old thread and not specific to Windows UWP apps.
I'm unable to set custom header in WebView so the loaded URLs in WebView could work for me.
I have seen many forums giving solution like using HttpClient/WebRequest with header but that doesn't work as in my case, the web address usage Javascript for redirection and before redirection it needs few custom header to load correctly.
Also WebView.NavigateWithHttpRequestMessage is not so suitable as it will postback and I need the headers for each request including javascript redirected URLs in web View.
I'm able to set custom headers in Xamarin.Droid project using Renderers but I couldn't find any solution for UWP Windows.UI.Xaml.Controls.WebView.
On Universal Windows 10 Platform, the WebView.NavigateWithHttpRequestMessage method is the right way.
a. I need the headers for each request including javascript redirected URLs in web View.
b. This didn't resolve my issue as after setting the headers the OnWebViewNavigationStarting method is called multiple times and App crashes automatically with System.StackOverflowException error
This is due to the infinite navigation will happen if we do navigation in the NavigationStarting event. We should cancel navigation in a handler for this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true.
And we need to add/remove handler for NavigationStarting event carefully.
Code sample:
private void NavigateWithHeader(Uri uri)
{
var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, uri);
requestMsg.Headers.Add("User-Name", "Franklin Chen");
wb.NavigateWithHttpRequestMessage(requestMsg);
wb.NavigationStarting += Wb_NavigationStarting;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigateWithHeader(new Uri("http://openszone.com/RedirectPage.html"));
}
private void Wb_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
wb.NavigationStarting -= Wb_NavigationStarting;
args.Cancel = true;//cancel navigation in a handler for this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true
NavigateWithHeader(args.Uri);
}
The screenshot is the log info in Fiddler, the request record in the second red rectangle included the custom header:
I shared my UWP sample in here, you can easily integrate into your Xamarin UWP app.
With the Xamarin Tag, it seems like you are using this with Xamarin.Forms and hence the below answer is with respect to Xamarin.Forms.
However, the code holds true for WebView in UWP as well.
You can try creating a Custom Renderer for WebView and then try making use of the same WebView.NavigateWithHttpRequestMessage.
Before navigating you can try setting the Headers like this:
var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, new Uri("https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending"));
requestMsg.Headers.Add("User-Name", "AnubhavRanjan");
Control.NavigateWithHttpRequestMessage(requestMsg);
The Uri above can be set based on your requirement.
In case the request is happening multiple times, you can always set the delegate for NavigationStarting event and handle it in the method.
Control.NavigationStarting += OnWebViewNavigationStarting
I'm currently doing some cross-platform mobile development through Visual Studio using Xamarin (so in C#) and am about to start the iOS portion. I've never done iOS development before and thought I could get myself acquainted with their "Hello, iOS" Tutorials. Unfortunately, things have not been going smoothly. I constantly get NSInvalidArgumentExceptions from my TouchUpInside actions:
Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSInvalidArgumentException Reason:
-[ViewController TranslateButton_TouchUpInside:]:
unrecognized selector sent to instance 0x7b6200d0
I can occasionally remedy it for a moment by literally remaking the Buttons, but it breaks pretty much right afterwards. The actual error itself occurs in my Main.cs file:
using UIKit;
namespace CheckinIOS
{
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate"); //this line is where it breaks
}
}
}
In case it is any helpful, I am trying to deploy to iPhone 5S simulator running iOS 9.3 (but it breaks on iPhone 6 simulator as well). I could also post more of my code if necessary, but I copypasted all the C# from Xamarin's tutorial, and did the same thing as them for Main.storyboard.
I have spent a while looking for people with the same problem as me, but their solutions either did not work, or they got the error for slightly different reasons. Any assistance is appreciated.
EDIT: Here is my implementation of TranslateButton_TouchUpInside:
TranslateButton.TouchUpInside += (object sender, EventArgs e) =>
{
// Convert the phone number with text to a number
// using PhoneTranslator.cs
translatedNumber = PhoneTranslator.ToNumber(PhoneNumberText.Text);
// Dismiss the keyboard if text field was tapped
PhoneNumberText.ResignFirstResponder();
if (translatedNumber == "")
{
CallButton.SetTitle("Call", UIControlState.Normal);
CallButton.Enabled = false;
}
else
{
CallButton.SetTitle("Call " + translatedNumber, UIControlState.Normal);
CallButton.Enabled = true;
}
};
The iOS Runtime is looking for a method called (in Obj-C land) TranslateButton_TouchUpInside: in your ViewController class. However there is no method exported to Obj-C with that name. A first guess is that you added an event to the button in the storyboard that perhaps had that name, but you either deleted that method or never implemented it.
Try opening your storyboard in iOS Designer and removing any event from the Properties->Events tab when your button is selected on the canvas. Also I assume your button has the name TranslateButton in the Properties->Widget pane when the button is selected on the canvas.
There are a couple ways to attach events to controls in Xamarin iOS. One, and the preferred way, is to create an event in iOS Designer for the control. If you do this, a partial method stub will be in the .designer.cs file with an Export attribute that exports the method name to the Obj-C runtime. You will then need to implement this method, using the same signature (without the Export Attribute), in your main .cs file for the ViewController. This is called, in Obj-C land, an action.
The other way is to do as is shown in your code snippet. In this case you ONLY need to give the control a name in the Properties->Widget pane that you can then use in code to subscribe to the TouchUpInside event. This is called, in Obj-C land, an outlet.
My guess is that you did both but without ever implementing the TranslateButton_TouchUpInside: method in your ViewController. Note that this is the Obj-C name used in the Export attribute of the method stub created in the .designer.cs file when you add an event to a control.
But it is hard to say without seeing the storyboard and both the main ViewController.cs file and the ViewController.designer.cs file
My windows phone app is freezing while loading the main page. I have set breakpoints on the main contructor and it fires the OnNavigatedTo event but never fires the "Loaded" event. It gets through the InitializeComponent() in the contructor. It shows the splash screen and the mainpage application bar, but freezes at that point.
I have recently refactored my main namespace, which was causing the mainpage to not load. I fixed that via the Startup Object.
EVERYTHING was working before the refactoring.
What code should I include? The project is fairly large at this point, so I dont know how much you really want me posting code.
Any ideas??
Ok I found the problem. In App.Xaml.cs I made the following crucial mistake:
Here is my code:
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != null && RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
And here is what is was supposed to be:
private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
{
// Set the root visual to allow the application to render
if (RootVisual != RootFrame)
RootVisual = RootFrame;
// Remove this handler since it is no longer needed
RootFrame.Navigated -= CompleteInitializePhoneApplication;
}
Resharper thought it would be best to check if RootVisual was null first. Incredibly irritating mistake as It would just hang at the startup PNG and never load, obviously because RootVisual wasn't being set.
I've had difficulties like this before. Sounds like it's probably a problem in your XAML but without actually having your solution it's hard to tell.
Here's a couple of things you could try:
Check all your static/dynamic resources to make sure that they're all still resolving correctly.
Check that any bindings and binding converters that you're using are still resolving properly. Converters in particular, I've had issues with them in the past.
If you're getting exceptions, but not tied to a specific point in code:
Try putting breakpoints on any or all custom properties that you might be using. Properties aren't stepped into by the debugger by default. I've found this to be the cause of a number of exceptions in my own work.
Hope this helps!
which event is rised up in Internet Explorer (IE9) when the F5 key (refresh) is clicked? And how can I catch it with an Handler in my BHO?
Note:
I have created a BHO in C# for IE9. My class extend IObjectWithSite that allow me to add handlers through SetSite function.
public int SetSite(object site)
{
webBrowser = (SHDocVw.WebBrowser)site;
//events here...
}
If you are developing a browser plugin that injects Javascript, I found it useful to hook both ondocumentcomplete and ondownloadcomplete.
Ondocumentcomplete fires as soon as the DOM has been loaded and can be manipulated, but it misses refreshes.
Ondownloadcomplete waits until all resources (e.g., images) have downloaded, but catches refreshes. This delay can be quite long.
By hooking both, you get a responsive plugin most of the time, and you don't miss refreshes. Your javascript can then include a check to avoid running twice. Something like:
// Inject the code, but only once
if (typeof myplugin == 'undefined') {
myplugin = new function () {
// Your code runs here.
};
}
I found the following page to be informative:
Alternative way to detect refresh in a BHO
There is no direct method and it is hard to implement across different versions of IE. Although you can use combination of some events to achieve that. Be warned the following approaches are not fool proof.
Links:
MSDN Forum
Detecting the IE Refresh button
Refresh and DISPID_DOCUMENTCOMPLETE
I am using the Business Silverlight application. I have incorporated some MVVM into this and were off an running with it. We are using some telerik controls, mostly the ribbon control and the docking. We register all the telerik ribbon controls in the about.xaml.cs file, the method is DisplayUI - its here where we register the docking control then we register the ribbon after this. What happens is that when you click the ABOUT link it shows our first tab with buttons(perfect). when you click the HOME link next to the ABOUT link, we go back to the home page..but when you click the ABOUT link again it registers the controls again so we end up with two tabs that are the same.
Is there a way to check to see if this about.xaml.cs file has already been initialized? Im guessing that is has a handle on the first call in memory as I am able to see the first tabs rendering..
Thanks
here is the about code
public About()
{
InitializeComponent();
DisplayUI();
this.Title = ApplicationStrings.AboutPageTitle;
}
that display UI does all the work in registering the dockpanel and the ribbons. We'd like to not have the DisplayUI() called if this has already been rendered once.
If you do it by event handler can you unsubscribe from the event at the end of the method? Without seeing some code it's hard to work out what to change.
It's not the nicest way of doing it, but if this code needs to run once and only once then you could have a static boolean variable on the class set to false and when you call DisplayUI you check the value of this. If it's false you set it to true and run the method, and if it's true you just return.