Inquiridor
Como Abrir um arquivo PDF em um Web View??

Discussão Geral
-
Olá Pessoas. o meu problema é o seguinte. Eu tenho um Aplicativo que mostra os horários de ônibus de algumas cidades do PR e alguns outros estados também. Só que eu queria fazer o seguinte:
-->No meu aplicativo tem um Web View, e eu gostaria de fazer um hyperlinkbutton para a página do PDF que fica no site do municipio (Como esse aqui: http://www.cettrans.com.br/arquivos/tcu/2016/22022016_175.pdf) que tem todas as linhas de ônibus aqui da cidade, e ao clicar nesse hyoerlinkbutton eu gostaria que ele fosse exibido dentro do webview da minha aplicação. Alguém sabe como que eu posso fazer isso? O que eu preciso alterar no código e onde exatamente e ou que mais preciso adicionar para ele abrir cada arquivo semelhante a esse do link acima dentro do webView? Segue o código que usei no Web View:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.UI.Xaml.Documents; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238 namespace Win8AppBox { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class oi : Page { public oi() { this.InitializeComponent(); address.KeyUp += address_KeyUp; webView1.NavigationStarting += webView1_NavigationStarting; webView1.ContentLoading += webView1_ContentLoading; webView1.DOMContentLoaded += webView1_DOMContentLoaded; webView1.UnviewableContentIdentified += webView1_UnviewableContentIdentified; webView1.NavigationCompleted += webView1_NavigationCompleted; } /// <summary> /// Invoked when this xaml page is about to be displayed in a Frame. /// Note: This event is not related to the webview navigation. /// </summary> /// <param name="e">Event data that describes how this page was reached. The Parameter /// property is typically used to configure the page.</param> protected override void OnNavigatedTo(NavigationEventArgs e) { address.Text = "http://cgn.uol.com.br/tag/3922/greve-transporte-coletivo-de-cascavel"; //tag para a página do transporte coletivo. } /// <summary> /// This handles the enter key in the url address box /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void address_KeyUp(object sender, KeyRoutedEventArgs e) { if (e.Key == Windows.System.VirtualKey.Enter) { NavigateWebview(address.Text); } } /// <summary> /// Helper to perform the navigation in webview /// </summary> /// <param name="url"></param> private void NavigateWebview(string url) { try { Uri targetUri = new Uri(url); webView1.Navigate(targetUri); } catch (FormatException myE) { // Bad address webView1.NavigateToString(String.Format("<h1>O endereço é inválido, tente novamente. Detales --> {0}.</h1>", myE.Message)); } } /// <summary> /// Property to control the "Go" button text, forward/backward buttons and progress ring /// </summary> private bool _pageIsLoading; bool pageIsLoading { get { return _pageIsLoading; } set { _pageIsLoading = value; goButton.Content = (value ? "Parar" : "Ir"); // progressRing1.Visibility = (value ? Visibility.Visible : Visibility.Collapsed); if (!value) { navigateBack.IsEnabled = webView1.CanGoBack; navigateForward.IsEnabled = webView1.CanGoForward; } } } /// <summary> /// Event to indicate webview is starting a navigation /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void webView1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) { string url = ""; try { url = args.Uri.ToString(); } finally { address.Text = url; appendLog(String.Format("Iniciando navegação para: \"{0}\".\n", url)); pageIsLoading = true; } } /// <summary> /// Event is fired by webview when the content is not a webpage, such as a file download /// </summary> /// <param name="sender"></param> /// <param name="args"></param> async void webView1_UnviewableContentIdentified(WebView sender, WebViewUnviewableContentIdentifiedEventArgs args) { appendLog(String.Format("Conteúdo para \"{0}\" não pode ser carregado no WebView. Chamando seu navegador padrão instalado.\n", args.Uri.ToString())); // We turn around and hand the Uri to the system launcher to launch the default handler for it await Windows.System.Launcher.LaunchUriAsync(args.Uri); pageIsLoading = false; } /// <summary> /// Event to indicate webview has resolved the uri, and that it is loading html content /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void webView1_ContentLoading(WebView sender, WebViewContentLoadingEventArgs args) { string url = (args.Uri != null) ? args.Uri.ToString() : "<null>"; appendLog(String.Format("Carregando conteúdo para \"{0}\".\n", url)); } /// <summary> /// Event to indicate that the content is fully loaded in the webview. If you need to invoke script, it is best to wait for this event. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void webView1_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args) { string url = (args.Uri != null) ? args.Uri.ToString() : "<null>"; appendLog(String.Format("Conteúdo para \"{0}\" terminou de ser carregado.\n", url)); } /// <summary> /// Event to indicate webview has completed the navigation, either with success or failure. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> void webView1_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) { pageIsLoading = false; if (args.IsSuccess) { string url = (args.Uri != null) ? args.Uri.ToString() : "<null>"; appendLog(String.Format("Navegação para \"{0}\"completada com sucesso.\n", url)); } else { string url = ""; try { url = args.Uri.ToString(); } finally { address.Text = url; appendLog(String.Format("A Navigação para: \"{0}\" encontrou um erro! Tente novamente. {1}.\n", url, args.WebErrorStatus.ToString())); appendLog(String.Format("Verifique sua conexão com a Internet.\n")); } } } /// <summary> /// Helper for logging /// </summary> /// <param name="logEntry"></param> void appendLog(string logEntry) { Run r = new Run(); r.Text = logEntry; Paragraph p = new Paragraph(); p.Inlines.Add(r); //logResults.Blocks.Add(p); } /*Botões Ir, Voltar e Avançar o Web View*/ private void Button_Click(object sender, RoutedEventArgs e) //Go button = IR { if (!pageIsLoading) { NavigateWebview(address.Text); } else { webView1.Stop(); pageIsLoading = false; } } private void navigateBack_Click(object sender, RoutedEventArgs e) //Voltar = navigateBack { if (webView1.CanGoBack) webView1.GoBack(); } private void navigateForward_Click(object sender, RoutedEventArgs e) //avançar = navigateForward { if (webView1.CanGoForward) webView1.GoForward(); } private void Principal_Click(object sender, RoutedEventArgs e) //retorna para a página principal do Aplicativo { Frame.Navigate(typeof(Principal)); } private void Button_Click_1(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(HappyHour)); } /* */ } }
- Tipo Alterado Marcos SJ sexta-feira, 26 de fevereiro de 2016 13:50 How to