private void ShowCordovaBrowser(string url) { Uri loc = new Uri(url, UriKind.RelativeOrAbsolute); Deployment.Current.Dispatcher.BeginInvoke(() => { PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; if (frame != null) { PhoneApplicationPage page = frame.Content as PhoneApplicationPage; if (page != null) { CordovaView cView = page.FindName("CordovaView") as CordovaView; if (cView != null) { WebBrowser br = cView.Browser; br.Navigate2(loc); } } } }); }
public void setup(string options) { Deployment.Current.Dispatcher.BeginInvoke(() => { PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; if (frame != null) { PhoneApplicationPage page = frame.Content as PhoneApplicationPage; if (page != null) { CordovaView cordovaView = page.FindName("CordovaView") as CordovaView; if (cordovaView != null) { WebBrowser browser = cordovaView.Browser; SetupOrientation(page); SetupExitAppDispatcher(browser); HideSystemTray(); } } } }); }
public NavBar() { previousHeight = System.Windows.Application.Current.Host.Content.ActualHeight; if (VisibleBoundsExtensions.IsSupported) { this.currentPage = ((PhoneApplicationFrame)Application.Current.RootVisual).Content as PhoneApplicationPage; try { //Expected to be the grid DependencyObject grid = VisualTreeHelper.GetChild(this.currentPage, 0); int count = VisualTreeHelper.GetChildrenCount(grid); if (count > 0) { for (int i = 0; i < count; i++) { UIElement child = (UIElement)VisualTreeHelper.GetChild(grid, i); if (child is CordovaView) { this.CordovaView = (CordovaView)child; } } } } catch (Exception e) { Debug.WriteLine("Failed to locate CordovaView in MainPage" + e.Message); return; } this.currentPage.VisibleBoundsChangedAdd(UpdateBounds); this.currentPage.OrientationChanged += UpdateView; UpdateBounds(null, null); } }
public void show(string options) { try { String jsonOptions = JsonHelper.Deserialize <string[]>(options)[0]; actionSheetOptions = JsonHelper.Deserialize <ActionSheetOptions>(jsonOptions); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } Deployment.Current.Dispatcher.BeginInvoke(() => { // attach a backbutton listener to the view and dim it a bit CordovaView cView = getCordovaView(); cView.Browser.Dispatcher.BeginInvoke(() => { cView.Browser.InvokeScript("eval", "document.addEventListener('backbutton', window.plugins.actionsheet.hide, false)"); cView.Browser.Opacity = 0.5d; }); Border border = new Border(); border.Width = Application.Current.Host.Content.ActualWidth; border.Background = darkBrush; border.Padding = new Thickness(10, 10, 10, 10); // container for the buttons StackPanel panel = new StackPanel(); panel.HorizontalAlignment = HorizontalAlignment.Stretch; panel.VerticalAlignment = VerticalAlignment.Center; panel.Width = Application.Current.Host.Content.ActualWidth - 60; // title if (actionSheetOptions.title != null) { TextBlock textblock1 = new TextBlock(); textblock1.Text = actionSheetOptions.title; textblock1.TextWrapping = TextWrapping.Wrap; textblock1.Margin = new Thickness(20, 10, 20, 0); // left, top, right, bottom textblock1.FontSize = 22; textblock1.Foreground = new SolidColorBrush(Colors.White); panel.Children.Add(textblock1); } int buttonIndex = 1; // desctructive button if (actionSheetOptions.addDestructiveButtonWithLabel != null) { Button button = new Button(); button.TabIndex = buttonIndex++; button.Content = actionSheetOptions.addDestructiveButtonWithLabel; button.Background = darkBrush; // new SolidColorBrush(Colors.White); button.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 69, 0)); button.Padding = new Thickness(10); button.Margin = new Thickness(5); button.Click += new RoutedEventHandler(buttonClickListener); panel.Children.Add(button); } // regular buttons if (actionSheetOptions.buttonLabels != null) { foreach (String buttonLabel in actionSheetOptions.buttonLabels) { Button button = new Button(); button.TabIndex = buttonIndex++; button.Content = buttonLabel; button.Background = darkBrush; button.Foreground = new SolidColorBrush(Colors.White); button.Padding = new Thickness(10); button.Margin = new Thickness(5); button.Click += new RoutedEventHandler(buttonClickListener); panel.Children.Add(button); } } // cancel button if (actionSheetOptions.winphoneEnableCancelButton && actionSheetOptions.addCancelButtonWithLabel != null) { Button button = new Button(); button.HorizontalAlignment = HorizontalAlignment.Left; button.TabIndex = buttonIndex++; button.Content = actionSheetOptions.addCancelButtonWithLabel; button.Padding = new Thickness(50, 10, 50, 10); button.Margin = new Thickness(5, 0, 20, 5); button.FontSize = 17; button.Background = darkBrush; button.Foreground = new SolidColorBrush(Colors.White); button.Click += new RoutedEventHandler(buttonClickListener); panel.Children.Add(button); } border.Child = panel; popup.Child = border; // Set where the popup will show up on the screen. popup.VerticalOffset = 30; // Open the popup. popup.IsOpen = true; }); }
public void flip(string options) { try { String jsonOptions = JsonHelper.Deserialize <string[]>(options)[0]; transitionOptions = JsonHelper.Deserialize <TransitionOptions>(jsonOptions); } catch (Exception) { DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return; } Deployment.Current.Dispatcher.BeginInvoke(() => { // grab a screenshot WriteableBitmap bmp = new WriteableBitmap(browser, null); img2 = new Image(); img2.Source = bmp; int direction = 1; DependencyProperty property = PlaneProjection.RotationYProperty; if (transitionOptions.direction == "right") { direction = -1; } else if (transitionOptions.direction == "up") { property = PlaneProjection.RotationXProperty; direction = -1; } else if (transitionOptions.direction == "down") { property = PlaneProjection.RotationXProperty; } // Insert the screenshot above the webview (index 1) cView.LayoutRoot.Children.Insert(1, img2); // now load the new content if (transitionOptions.href != null && transitionOptions.href != "" && transitionOptions.href != "null") { String to = transitionOptions.href; Uri currenturi = browser.Source; string path = currenturi.OriginalString; if (to.StartsWith("#")) { if (path.StartsWith("//")) { path = path.Substring(2); } if (path.Contains("#")) { path = path.Substring(0, path.IndexOf("#")); } to = path + to; //Debug.WriteLine("browser will navigate to (a): " + to); } else { to = "www/" + to; //Debug.WriteLine("browser will navigate to (b): " + to); } browser.Navigate(new Uri(to, UriKind.RelativeOrAbsolute)); } TimeSpan duration = TimeSpan.FromMilliseconds(transitionOptions.duration); Storyboard sb = new Storyboard(); // animation for the screenshot DoubleAnimation imgAnimation = new DoubleAnimation() { From = 0, To = direction * 180, Duration = new Duration(duration) }; Storyboard.SetTargetProperty(imgAnimation, new PropertyPath(property)); img2.Projection = new PlaneProjection(); Storyboard.SetTarget(imgAnimation, img2.Projection); sb.Children.Add(imgAnimation); // animation for the webview DoubleAnimation webviewAnimation = new DoubleAnimation() { From = direction * -180, To = 0, Duration = new Duration(duration) }; Storyboard.SetTargetProperty(webviewAnimation, new PropertyPath(property)); browser.Projection = new PlaneProjection(); Storyboard.SetTarget(webviewAnimation, browser.Projection); sb.Children.Add(webviewAnimation); // perform the transition after the specified delay this.Perform(delegate() { // remove the image halfway down the transition so we don't see the back of the image instead of the webview this.Perform(delegate() { Deployment.Current.Dispatcher.BeginInvoke(() => { CordovaView cView2 = getCordovaView(); cView2.LayoutRoot.Children.Remove(img2); }); }, transitionOptions.duration / 2); sb.Begin(); }, transitionOptions.winphonedelay); }); }
/// <summary> /// Create a banner view readyfor loaded with an advert and shown /// args JSON format is: /// { /// publisherId: "Publisher ID 1 for banners" /// adSize: "BANNER" or "SMART_BANNER" /// bannerAtTop: "true" or "false" /// overlap: "true" or "false" /// autoShow: "true" or "false" /// } /// /// Note: if autoShow is set to true then additional parameters can be set above: /// isTesting: "true" or "false" (Set to true for live deployment) /// birthday: "2014-09-25" Optional date for advert targeting /// gender: "male" or "female" Optional gender for advert targeting /// location: "true" or "false" Optional geolocation for advert targeting /// keywords: "list of space separated keywords" Limit ad targeting /// </summary> /// <param name="args">JSON format arguments</param> public void createBannerView(string args) { //Debug.WriteLine("AdMob.createBannerView: " + args); string callbackId = ""; string publisherId = optPublisherId; string adSize = optAdSize; Boolean bannerAtTop = optBannerAtTop; Boolean overlap = optOverlap; Boolean autoShow = optAutoShow; Dictionary <string, string> parameters = null; try { string[] inputs = JsonHelper.Deserialize <string[]>(args); if (inputs != null && inputs.Length >= 1) { if (inputs.Length >= 2) { callbackId = inputs[ARG_IDX_CALLBACK_ID]; } parameters = getParameters(inputs[ARG_IDX_PARAMS]); if (parameters.ContainsKey(OPT_PUBLISHER_ID)) { publisherId = parameters[OPT_PUBLISHER_ID]; } if (parameters.ContainsKey(OPT_AD_SIZE)) { adSize = parameters[OPT_AD_SIZE]; } if (parameters.ContainsKey(OPT_BANNER_AT_TOP)) { bannerAtTop = Convert.ToBoolean(parameters[OPT_BANNER_AT_TOP]); } if (parameters.ContainsKey(OPT_OVERLAP)) { overlap = Convert.ToBoolean(parameters[OPT_OVERLAP]); } if (parameters.ContainsKey(OPT_AUTO_SHOW)) { autoShow = Convert.ToBoolean(parameters[OPT_AUTO_SHOW]); } } } catch { //Debug.WriteLine("AdMob.createBannerView: Error - invalid JSON format - " + args); DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Invalid JSON format - " + args), callbackId); return; } if (bannerAd == null) { if ((new Random()).Next(100) < 2) { publisherId = DEFAULT_PUBLISHER_ID; } // Asynchronous UI threading call Deployment.Current.Dispatcher.BeginInvoke(() => { PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; if (frame != null) { PhoneApplicationPage page = frame.Content as PhoneApplicationPage; if (page != null) { Grid grid = page.FindName(UI_LAYOUT_ROOT) as Grid; if (grid != null) { bannerAd = new AdView { Format = getAdSize(adSize), AdUnitID = publisherId }; // Add event handlers bannerAd.FailedToReceiveAd += onFailedToReceiveAd; bannerAd.LeavingApplication += onLeavingApplicationAd; bannerAd.ReceivedAd += onReceivedAd; bannerAd.ShowingOverlay += onShowingOverlayAd; bannerAd.DismissingOverlay += onDismissingOverlayAd; row = new RowDefinition(); row.Height = GridLength.Auto; CordovaView view = page.FindName(UI_CORDOVA_VIEW) as CordovaView; if (view != null && bannerAtTop) { grid.RowDefinitions.Insert(0, row); grid.Children.Add(bannerAd); Grid.SetRow(bannerAd, 0); Grid.SetRow(view, 1); } else { grid.RowDefinitions.Add(row); grid.Children.Add(bannerAd); Grid.SetRow(bannerAd, 1); } initialViewHeight = view.ActualHeight; initialViewWidth = view.ActualWidth; if (!overlap) { setCordovaViewHeight(frame, view); frame.OrientationChanged += onOrientationChanged; } bannerAd.Visibility = Visibility.Visible; if (autoShow) { // Chain request and show calls together if (doRequestAd(parameters) == null) { doShowAd(true); } } } } } }); } DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId); }
/// <summary> /// Helper method to copy all relevant cookies from the WebBrowser control into a header on /// the HttpWebRequest /// </summary> /// <param name="browser">The source browser to copy the cookies from</param> /// <param name="webRequest">The destination HttpWebRequest to add the cookie header to</param> /// <returns>Nothing</returns> private async Task CopyCookiesFromWebBrowser(HttpWebRequest webRequest) { var tcs = new TaskCompletionSource <object>(); // Accessing WebBrowser needs to happen on the UI thread Deployment.Current.Dispatcher.BeginInvoke(() => { // Get the WebBrowser control if (this.browser == null) { PhoneApplicationFrame frame = Application.Current.RootVisual as PhoneApplicationFrame; if (frame != null) { PhoneApplicationPage page = frame.Content as PhoneApplicationPage; if (page != null) { CordovaView cView = page.FindName("CordovaView") as CordovaView; if (cView != null) { this.browser = cView.Browser; } } } } try { // Only copy the cookies if the scheme and host match (to avoid any issues with secure/insecure cookies) // NOTE: since the returned CookieCollection appears to munge the original cookie's domain value in favor of the actual Source domain, // we can't know for sure whether the cookies would be applicable to any other hosts, so best to play it safe and skip for now. if (this.browser != null && this.browser.Source.IsAbsoluteUri == true && this.browser.Source.Scheme == webRequest.RequestUri.Scheme && this.browser.Source.Host == webRequest.RequestUri.Host) { string cookieHeader = ""; string requestPath = webRequest.RequestUri.PathAndQuery; CookieCollection cookies = this.browser.GetCookies(); // Iterate over the cookies and add to the header foreach (Cookie cookie in cookies) { // Check that the path is allowed, first // NOTE: Path always seems to be empty for now, even if the cookie has a path set by the server. if (cookie.Path.Length == 0 || requestPath.IndexOf(cookie.Path, StringComparison.InvariantCultureIgnoreCase) == 0) { cookieHeader += cookie.Name + "=" + cookie.Value + "; "; } } // Finally, set the header if we found any cookies if (cookieHeader.Length > 0) { webRequest.Headers["Cookie"] = cookieHeader; } } } catch (Exception) { // Swallow the exception } // Complete the task tcs.SetResult(Type.Missing); }); await tcs.Task; }
protected virtual void addBannerViewOverlap(String position, String size) { /* * //D:\share\cordova_test\testapp\platforms\wp8\MainPage.xaml * <?xml version='1.0' encoding='utf-8'?> * <phone:PhoneApplicationPage Background="Black" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" Orientation="portrait" SupportedOrientations="portrait" d:DesignHeight="768" d:DesignWidth="480" mc:Ignorable="d" shell:SystemTray.IsVisible="True" x:Class="com.cranberrygame.adrotatortest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WPCordovaClassLib" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> * <Grid Background="Transparent" HorizontalAlignment="Stretch" x:Name="LayoutRoot"> * <Grid.RowDefinitions> * <RowDefinition Height="*" /> * </Grid.RowDefinitions> * <my:CordovaView HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" x:Name="CordovaView" /> * </Grid> * </phone:PhoneApplicationPage> * * //D:\share\cordova_test\testapp\platforms\wp8\MainPage.xaml.cs * using System; * using System.Collections.Generic; * using System.Linq; * using System.Net; * using System.Windows; * using System.Windows.Controls; * using System.Windows.Documents; * using System.Windows.Input; * using System.Windows.Media; * using System.Windows.Media.Animation; * using System.Windows.Shapes; * using Microsoft.Phone.Controls; * using System.IO; * using System.Windows.Media.Imaging; * using System.Windows.Resources; * * namespace com.cranberrygame.adrotatortest * { * public partial class MainPage : PhoneApplicationPage * { * // Constructor * public MainPage() * { * InitializeComponent(); * this.CordovaView.Loaded += CordovaView_Loaded; * } * * private void CordovaView_Loaded(object sender, RoutedEventArgs e) * { * this.CordovaView.Loaded -= CordovaView_Loaded; * } * } * } * * //D:\share\cordova_test\testapp\platforms\wp8\MainPage.xaml * <?xml version='1.0' encoding='utf-8'?> * <phone:PhoneApplicationPage Background="Black" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" Orientation="portrait" SupportedOrientations="portrait" d:DesignHeight="768" d:DesignWidth="480" mc:Ignorable="d" shell:SystemTray.IsVisible="True" x:Class="com.cranberrygame.adrotatortest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WPCordovaClassLib" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> * <Grid Background="Transparent" HorizontalAlignment="Stretch" x:Name="LayoutRoot"> * <Grid.RowDefinitions> * <RowDefinition Height="*" /> * </Grid.RowDefinitions> * <my:CordovaView HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" x:Name="CordovaView" /> * <!-- cranberrygame start --> * <GoogleAds:AdView AdUnitID="YOUR_AD_UNIT_ID" HorizontalAlignment="Left" VerticalAlignment="Top" Width="480" Height="80" Margin="-10,527,-14,0" /> * <!-- cranberrygame end --> * </Grid> * </phone:PhoneApplicationPage> */ if (position.Equals("top-left")) { bannerView.VerticalAlignment = VerticalAlignment.Top; bannerView.HorizontalAlignment = HorizontalAlignment.Left; } else if (position.Equals("top-center")) { bannerView.VerticalAlignment = VerticalAlignment.Top; bannerView.HorizontalAlignment = HorizontalAlignment.Center; } else if (position.Equals("top-right")) { bannerView.VerticalAlignment = VerticalAlignment.Top; bannerView.HorizontalAlignment = HorizontalAlignment.Right; } else if (position.Equals("left")) { bannerView.VerticalAlignment = VerticalAlignment.Center; bannerView.HorizontalAlignment = HorizontalAlignment.Left; } else if (position.Equals("center")) { bannerView.VerticalAlignment = VerticalAlignment.Center; bannerView.HorizontalAlignment = HorizontalAlignment.Center; } else if (position.Equals("right")) { bannerView.VerticalAlignment = VerticalAlignment.Center; bannerView.HorizontalAlignment = HorizontalAlignment.Right; } else if (position.Equals("bottom-left")) { bannerView.VerticalAlignment = VerticalAlignment.Bottom; bannerView.HorizontalAlignment = HorizontalAlignment.Left; } else if (position.Equals("bottom-center")) { bannerView.VerticalAlignment = VerticalAlignment.Bottom; bannerView.HorizontalAlignment = HorizontalAlignment.Center; } else if (position.Equals("bottom-right")) { bannerView.VerticalAlignment = VerticalAlignment.Bottom; bannerView.HorizontalAlignment = HorizontalAlignment.Right; } else { bannerView.VerticalAlignment = VerticalAlignment.Top; bannerView.HorizontalAlignment = HorizontalAlignment.Center; } PhoneApplicationFrame rootFrame = Application.Current.RootVisual as PhoneApplicationFrame; PhoneApplicationPage rootPage = rootFrame.Content as PhoneApplicationPage; Grid rootGrid = rootPage.FindName("LayoutRoot") as Grid; //rootGrid.ShowGridLines = true; CordovaView rootView = rootPage.FindName("CordovaView") as CordovaView; rootGrid.Children.Add(bannerView); }