public static void OnCallbackFromJavaScriptError(string idWhereCallbackArgsAreStored) { Action action = () => { string errorMessage = Convert.ToString(Interop.ExecuteJavaScript("document.jsObjRef[$0][0]", idWhereCallbackArgsAreStored)); int indexOfNextUnmodifiedJSCallInList = Convert.ToInt32(Interop.ExecuteJavaScript("document.jsObjRef[$0][1]", idWhereCallbackArgsAreStored)); INTERNAL_InteropImplementation.ShowErrorMessage(errorMessage, indexOfNextUnmodifiedJSCallInList); }; #if OPENSILVER if (Interop.IsRunningInTheSimulator_WorkAround) #else if (Interop.IsRunningInTheSimulator) #endif { // Go back to the UI thread because DotNetBrowser calls the callback from the socket background thread: INTERNAL_Simulator.WebControlDispatcherBeginInvoke(action); } else { action(); } }
public Application() { // Keep a reference to the app: Application.Current = this; // Initialize Deployment _ = Deployment.Current; // In case of a redirection from Microsoft AAD, when running in the Simulator, we re-instantiate the application. We need to reload the JavaScript files because they are no longer in the HTML DOM due to the AAD redirection: INTERNAL_InteropImplementation.ResetLoadedFilesDictionaries(); #if CSHTML5BLAZOR // we change the resource manager for every resource registered ClientSideResourceRegister.Startup(); #endif // Keep a reference to the startup assembly: StartupAssemblyInfo.StartupAssembly = this.GetType().Assembly; // Remember whether we are in "SL Migration" mode or not: #if MIGRATION CSHTML5.Interop.ExecuteJavaScript(@"document.isSLMigration = true"); #else CSHTML5.Interop.ExecuteJavaScript(@"document.isSLMigration = false"); #endif //Interop.ExecuteJavaScript("document.raiseunhandledException = $0", (Action<object>)RaiseUnhandledException); // Inject the "DataContractSerializer" into the "XmlSerializer" (read note in the "XmlSerializer" implementation to understand why): #if OPENSILVER if (false) //Note: in case of the Simulator, we reference the .NET Framework version of "System.xml.dll", so we cannot inject stuff because the required members of XmlSerializer would be missing. #elif BRIDGE if (!CSHTML5.Interop.IsRunningInTheSimulator) //Note: in case of the Simulator, we reference the .NET Framework version of "System.xml.dll", so we cannot inject stuff because the required members of XmlSerializer would be missing. #endif { InjectDataContractSerializerIntoXmlSerializer(); } #if !CSHTML5NETSTANDARD // Fix the freezing of the Simulator when calling 'alert' using the "Interop.ExecuteJavaScript()" method by redirecting the JavaScript "alert" to the Simulator message box: if (CSHTML5.Interop.IsRunningInTheSimulator) { RedirectAlertToMessageBox_SimulatorOnly(); } #endif // Get default font-family from css INTERNAL_FontsHelper.DefaultCssFontFamily = Convert.ToString(CSHTML5.Interop.ExecuteJavaScript("window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue(\"font-family\")")); TextMeasurementService = new TextMeasurementService(); // Initialize the window: if (_mainWindow == null) // Note: it could be != null if the user clicks "Restart" from the Simulator advanced options. { _mainWindow = new Window(); Window.Current = _mainWindow; object applicationRootDomElement = INTERNAL_HtmlDomManager.GetApplicationRootDomElement(); _mainWindow.AttachToDomElement(applicationRootDomElement); // Listen to clicks anywhere in the window (this is used to close the popups that are not supposed to stay open): #if MIGRATION _mainWindow.AddHandler(UIElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(INTERNAL_PopupsManager.OnClickOnPopupOrWindow), true); #else _mainWindow.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(INTERNAL_PopupsManager.OnClickOnPopupOrWindow), true); #endif #if !CSHTML5NETSTANDARD // Workaround an issue on Firefox where the UI disappears if the window is resized and on some other occasions: if (INTERNAL_HtmlDomManager.IsFirefox()) { _mainWindow.SizeChanged += MainWindow_SizeChanged; _timerForWorkaroundFireFoxIssue.Interval = new TimeSpan(0, 0, 2); _timerForWorkaroundFireFoxIssue.Tick += TimerForWorkaroundFireFoxIssue_Tick; } #endif } // We call the "Startup" event and the "OnLaunched" method using the Dispatcher, because usually the user registers the "Startup" event in the constructor of the "App.cs" class, which is derived from "Application.cs", and therefore when we arrive here the event is not yet registered. Executing the code in the Dispatcher ensures that the constructor of the "App.cs" class has finished before running the code. #if MIGRATION Dispatcher #else CoreDispatcher #endif .INTERNAL_GetCurrentDispatcher().BeginInvoke((Action)(() => { StartAppServices(); // Raise the "Startup" event: if (this.Startup != null) { Startup(this, new StartupEventArgs()); } // Call the "OnLaunched" method: this.OnLaunched(new LaunchActivatedEventArgs()); })); }
private static void Source_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var control = (MediaElement)d; var newValue = (Uri)e.NewValue; // Always check that the control is in the Visual Tree before modifying its HTML representation if (INTERNAL_VisualTreeManager.IsElementInVisualTree(control)) { string newUri = newValue.ToString(); // If the new Source is an empty string, we avoid the error messages: if (!string.IsNullOrWhiteSpace(newUri)) { string tagString = "none"; string valueForHtml5SourceProperty = INTERNAL_UriHelper.ConvertToHtml5Path(newValue.ToString(), control); string newExtensionLowercase = GetExtension(newUri).ToLower(); if (SupportedVideoTypes.Contains(newExtensionLowercase)) { if (control.IsAudioOnly || control._mediaElement == null) //note: I chose to use IsAudioOnly here because using e.oldValue would make it recreate the video tag when it was already a video tag. { tagString = "video"; control.IsAudioOnly = false; } } else if (SupportedAudioTypes.Contains(newExtensionLowercase)) { if (!control.IsAudioOnly || control._mediaElement == null) //note: I chose to use IsAudioOnly here because using e.oldValue would make it recreate the audio tag when it was already a audio tag. { tagString = "audio"; control.IsAudioOnly = true; } } else { throw new NotSupportedException("ERROR: The MediaElement control only supports files of the following types: VIDEO: mp4, ogv, webm, 3gp - AUDIO: mp3, ogg - Note: for best browser compatibility, it is recommended to use only MP3 and MP4 files."); } if (tagString != "none") //tagString != "none" means that the new Uri has a different type (audio VS video) than the old one, so we need to (re)create the dom tag. { if (control._mediaElement != null) { INTERNAL_HtmlDomManager.RemoveFromDom(control._mediaElement); //note: there can be only one child element. } #if OPENSILVER if (!INTERNAL_InteropImplementation.IsRunningInTheSimulator_WorkAround() || tagString == "video") #else if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript || tagString == "video") #endif { object element = null; object outerDiv = control.INTERNAL_OuterDomElement; var elementStyle = INTERNAL_HtmlDomManager.CreateDomElementAppendItAndGetStyle(tagString, outerDiv, control, out element); control._mediaElement_ForAudioOnly_ForSimulatorOnly = null; control._mediaElement = element; if (tagString == "video") { elementStyle.width = "100%"; elementStyle.height = "100%"; } control.Refresh(); //we refresh all the values of the element in the visual tree } else //when we are in the simulator, we don't want to use the <audio> tag, we will use a wpf one instead (because awesomium doesn't support .mp3 for example) { #if !CSHTML5NETSTANDARD if (control._mediaElement_ForAudioOnly_ForSimulatorOnly == null) { control._mediaElement_ForAudioOnly_ForSimulatorOnly = INTERNAL_Simulator.WpfMediaElementFactory.Create((Action)control.SimulatorMediaElement_Loaded, (Action)control.SimulatorMediaElement_MediaEnded); } control._mediaElement_ForAudioOnly_ForSimulatorOnly.Source = new Uri(valueForHtml5SourceProperty); control.Refresh_SimulatorOnly(); #endif return; } } // Update the "src" property of the <video> or <audio> tag INTERNAL_HtmlDomManager.SetDomElementAttribute(control._mediaElement, "src", valueForHtml5SourceProperty, forceSimulatorExecuteImmediately: true); } else { if (control._mediaElement != null) { // Remove previous video/audio if any: INTERNAL_HtmlDomManager.SetDomElementAttribute(control._mediaElement, "src", "", forceSimulatorExecuteImmediately: true); } } } }