locked
How to bind the webview RRS feed

  • Question

  • Hi,

    I am creating an application where i am showing multiple webview.I have an array of WebViews so i am putting it on for loop.

    here is my code.

                                 

       C#  code

     for (int i = 0; i < chart.Count; i++)
                        {
                           String Charturi = String.Format("{0}/contentserver/services/content/analytics/{1}/html?token={2}", base_url, chart[i].AnalyticID, token);
                           Webview1.Source = new Uri(Charturi);

                        }

    XAML code

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <!--<GridView HorizontalAlignment="Left" Margin="195,213,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto">-->
            <StackPanel HorizontalAlignment="Left" Height="471" Margin="10,10,0,0"  VerticalAlignment="Top" Width="1333">
                <ListBox Height="453" Margin="350,0,35,0">

                    <WebView HorizontalAlignment="Left" x:Name="Webview1" Height="424" VerticalAlignment="Top" Width="832"  />

                </ListBox>
            </StackPanel>
            <!--</GridView>-->
            <Button Content="Back" HorizontalAlignment="Left" Margin="36,40,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>

        </Grid>

    please guide me on this.

    Thank you in advance.

    Thursday, October 24, 2013 8:55 AM

Answers

  • You need to use attached property to bind URL to WebView. Try the below code.

    XAML

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <!--<GridView HorizontalAlignment="Left" Margin="195,213,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto">-->
        <StackPanel HorizontalAlignment="Left" Height="471" Margin="10,10,0,0"  VerticalAlignment="Top" Width="1333">
            <ListBox x:Name="lbx" Height="453" Margin="350,0,35,0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <WebView local:WebViewExtensions.UriSource="{Binding}" HorizontalAlignment="Left" Height="424" VerticalAlignment="Top" Width="832" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
        <!--</GridView>-->
        <Button Content="Back" HorizontalAlignment="Left" Margin="36,40,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
    </Grid>

    C#

    public static class WebViewExtensions
    {
        public static string GetUriSource(WebView view)
        {
            return (string)view.GetValue(UriSourceProperty);
        }
    
        public static void SetUriSource(WebView view, string value)
        {
            view.SetValue(UriSourceProperty, value);
        }
    
        public static readonly DependencyProperty UriSourceProperty =
            DependencyProperty.RegisterAttached(
            "UriSource", typeof(string), typeof(WebViewExtensions),
            new PropertyMetadata(null, OnUriSourcePropertyChanged));
    
        private static void OnUriSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var webView = sender as WebView;
            if (webView == null)
                throw new NotSupportedException();
    
            if (e.NewValue != null)
            {
                var uri = new Uri(e.NewValue.ToString());
                webView.Navigate(uri);
            }
        }
    }
    

    var URIs = new List<string>();
    for (int i = 0; i < chart.Count; i++)
    {
        URIs.Add(String.Format("{0}/contentserver/services/content/analytics/{1}/html?token={2}", base_url, chart[i].AnalyticID, token));
    }
    lbx.ItemsSource = URIs;
    


    • Proposed as answer by Oleg Kurzov Thursday, October 24, 2013 9:43 AM
    • Marked as answer by Anne Jing Friday, October 25, 2013 1:08 AM
    Thursday, October 24, 2013 9:40 AM

All replies

  • You need to use attached property to bind URL to WebView. Try the below code.

    XAML

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <!--<GridView HorizontalAlignment="Left" Margin="195,213,0,0" VerticalAlignment="Top" Width="Auto" Height="Auto">-->
        <StackPanel HorizontalAlignment="Left" Height="471" Margin="10,10,0,0"  VerticalAlignment="Top" Width="1333">
            <ListBox x:Name="lbx" Height="453" Margin="350,0,35,0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <WebView local:WebViewExtensions.UriSource="{Binding}" HorizontalAlignment="Left" Height="424" VerticalAlignment="Top" Width="832" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
        <!--</GridView>-->
        <Button Content="Back" HorizontalAlignment="Left" Margin="36,40,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
    </Grid>

    C#

    public static class WebViewExtensions
    {
        public static string GetUriSource(WebView view)
        {
            return (string)view.GetValue(UriSourceProperty);
        }
    
        public static void SetUriSource(WebView view, string value)
        {
            view.SetValue(UriSourceProperty, value);
        }
    
        public static readonly DependencyProperty UriSourceProperty =
            DependencyProperty.RegisterAttached(
            "UriSource", typeof(string), typeof(WebViewExtensions),
            new PropertyMetadata(null, OnUriSourcePropertyChanged));
    
        private static void OnUriSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var webView = sender as WebView;
            if (webView == null)
                throw new NotSupportedException();
    
            if (e.NewValue != null)
            {
                var uri = new Uri(e.NewValue.ToString());
                webView.Navigate(uri);
            }
        }
    }
    

    var URIs = new List<string>();
    for (int i = 0; i < chart.Count; i++)
    {
        URIs.Add(String.Format("{0}/contentserver/services/content/analytics/{1}/html?token={2}", base_url, chart[i].AnalyticID, token));
    }
    lbx.ItemsSource = URIs;
    


    • Proposed as answer by Oleg Kurzov Thursday, October 24, 2013 9:43 AM
    • Marked as answer by Anne Jing Friday, October 25, 2013 1:08 AM
    Thursday, October 24, 2013 9:40 AM
  • Hi Xyroid,

    Thank you very very much.now it is working fine.It helps me a lot.


    Akash rao

    Thursday, October 24, 2013 10:23 AM