none
how to make variables in event handlers available to other methods on the same page?

    Question

  • Hi,

    I am newbie to C# and WP development. I have a simple page with three links that go to 3 different sites. I also have an app bar where I will provide different operations, such as email sharing and saving to isolated storage. I use a browser control to go to the three different sites by declaring a variable in the event handler itself. I am having problems have issues having these variables be available to my app bar event handlers. How do I make them available to other methods on the same page?

    here is an example of my XAML page:

    <phone:PhoneApplicationPage
        x:Class="O365_Content_App.test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
        mc:Ignorable="d"
        shell:SystemTray.IsVisible="True"
        ApplicationBar = "{StaticResource GlobalAppBar}">
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="#FF0072C6">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
    
            <!--TitlePanel contains the name of the application and page title-->
    
            <StackPanel Grid.Row="0" Margin="0,0,0,0" Background="#DC3C00">
                <Image x:Name="bannerImage" Source="images/logo_resourceguide.png" Margin="10,0,0,0" Stretch="None" VerticalAlignment="top" HorizontalAlignment="Left"/>
           
            </StackPanel>
    
          
            <ScrollViewer  VerticalScrollBarVisibility="Visible"> 
                
            <!--ContentPanel - place additional content here-->
             <Grid x:Name="ContentPanel" Margin="0,50,0,0" Background="#FF0072C6" Grid.RowSpan="2" Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
    
                
                <TextBlock x:Name="serviceHealth" Text="link1"  HorizontalAlignment="Left" Margin="10,10,0,0"  Grid.Row="0" TextWrapping="NoWrap"  VerticalAlignment="Bottom" Tap="link1_Tap" />
                 <TextBlock Text="link2" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link2_Tap"/>
                <TextBlock Text="link3" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link3_Tap"/>
               
                <!--<HyperlinkButton NavigateUri="HomePage.xaml" Content="Top Issues"  Grid.Row="7" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"  Style="{StaticResource  ResourceKey=HyperlinkButtonStyle}" />-->
                    <!--<HyperlinkButton NavigateUri="/HomePage.xaml" Content="Top Issues"  Grid.Row="7" />-->
    
    
                </Grid>
    
            </ScrollViewer>
            <phone:WebBrowser Grid.Row="0" Name="webBrowser1"  Visibility="Collapsed"  Width="auto" Height="auto" Grid.RowSpan="2"/>
    
        </Grid>
    
    
    </phone:PhoneApplicationPage>

    and here is my c# page:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Tasks;
    using System.Net.Http;
    using System.IO;
    using System.Windows.Media;
    using System.ComponentModel;
    
    namespace O365_Content_App
    {
        public partial class test : PhoneApplicationPage
        {
            public test()
            {
                InitializeComponent();
    
    
                ApplicationBar = new ApplicationBar();
                ApplicationBar.Mode = ApplicationBarMode.Default;
                ApplicationBar.Opacity = 1.0;
                ApplicationBar.IsVisible = true;
                ApplicationBar.IsMenuEnabled = true;
                ApplicationBar.BackgroundColor = Color.FromArgb(225, 220, 60, 0);
    
                ApplicationBarIconButton button1 = new ApplicationBarIconButton();
                button1.IconUri = new Uri("/Images/favs.addto.png", UriKind.Relative);
                button1.Text = "Add as Favorite";
                ApplicationBar.Buttons.Add(button1);
                button1.Click += new EventHandler(button1_Click);
    
                ApplicationBarIconButton button2 = new ApplicationBarIconButton();
                button2.IconUri = new Uri("/Images/feature.email.png", UriKind.Relative);
                button2.Text = "Email";
                ApplicationBar.Buttons.Add(button2);
                button2.Click += new EventHandler(button2_Click);
    
                ApplicationBarIconButton button3 = new ApplicationBarIconButton();
                button3.IconUri = new Uri("/Images/homeicon.png", UriKind.Relative);
                button3.Text = "Home";
                ApplicationBar.Buttons.Add(button3);
                button3.Click += new EventHandler(button3_Click);
          
            }
    
    
    
            private void link1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.microsoft.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
    
    
            }
    
            private void link2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.intel.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
    
            }
    
            private void link3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.ibm.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
            }
    
            
            protected override void OnBackKeyPress(CancelEventArgs e)
            {
                if (webBrowser1.CanGoBack)
                {
                    e.Cancel = true;
                    webBrowser1.GoBack();
                }
                else
                {
                    base.OnBackKeyPress(e);
                }
            }
    
    
    
            
            private void button1_Click(object sender, EventArgs e)
            {
                
                MessageBox.Show("Button 1 works!");
              //  MessageBox.Show(site);
               //Do work for your application here.
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
            
                
                MessageBox.Show("Button 2 works!");
             }
    
    
            private void button3_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Button 3 works!");
            }
    
    
    
      
    
        }
    }
    Monday, September 30, 2013 3:38 PM

All replies

  • Hi,

    I am newbie to C# and WP development. I have a simple page with three links that go to 3 different sites. I also have an app bar where I will provide different operations, such as email sharing and saving to isolated storage. I use a browser control to go to the three different sites by declaring a variable in the event handler itself. I am having problems have issues having these variables be available to my app bar event handlers. How do I make them available to other methods on the same page?

    here is an example of my XAML page:

    <phone:PhoneApplicationPage
        x:Class="O365_Content_App.test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
        mc:Ignorable="d"
        shell:SystemTray.IsVisible="True"
        ApplicationBar = "{StaticResource GlobalAppBar}">
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="#FF0072C6">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
    
            <!--TitlePanel contains the name of the application and page title-->
    
            <StackPanel Grid.Row="0" Margin="0,0,0,0" Background="#DC3C00">
                <Image x:Name="bannerImage" Source="images/logo_resourceguide.png" Margin="10,0,0,0" Stretch="None" VerticalAlignment="top" HorizontalAlignment="Left"/>
           
            </StackPanel>
    
          
            <ScrollViewer  VerticalScrollBarVisibility="Visible"> 
                
            <!--ContentPanel - place additional content here-->
             <Grid x:Name="ContentPanel" Margin="0,50,0,0" Background="#FF0072C6" Grid.RowSpan="2" Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
    
                
                <TextBlock x:Name="serviceHealth" Text="link1"  HorizontalAlignment="Left" Margin="10,10,0,0"  Grid.Row="0" TextWrapping="NoWrap"  VerticalAlignment="Bottom" Tap="link1_Tap" />
                 <TextBlock Text="link2" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link2_Tap"/>
                <TextBlock Text="link3" HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="2" TextWrapping="NoWrap"  VerticalAlignment="Top" Tap="link3_Tap"/>
               
                <!--<HyperlinkButton NavigateUri="HomePage.xaml" Content="Top Issues"  Grid.Row="7" VerticalContentAlignment="Top" HorizontalContentAlignment="Left"  Style="{StaticResource  ResourceKey=HyperlinkButtonStyle}" />-->
                    <!--<HyperlinkButton NavigateUri="/HomePage.xaml" Content="Top Issues"  Grid.Row="7" />-->
    
    
                </Grid>
    
            </ScrollViewer>
            <phone:WebBrowser Grid.Row="0" Name="webBrowser1"  Visibility="Collapsed"  Width="auto" Height="auto" Grid.RowSpan="2"/>
    
        </Grid>
    
    
    </phone:PhoneApplicationPage>

    and here is my c# page:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Phone.Tasks;
    using System.Net.Http;
    using System.IO;
    using System.Windows.Media;
    using System.ComponentModel;
    
    namespace O365_Content_App
    {
        public partial class test : PhoneApplicationPage
        {
            public test()
            {
                InitializeComponent();
    
    
                ApplicationBar = new ApplicationBar();
                ApplicationBar.Mode = ApplicationBarMode.Default;
                ApplicationBar.Opacity = 1.0;
                ApplicationBar.IsVisible = true;
                ApplicationBar.IsMenuEnabled = true;
                ApplicationBar.BackgroundColor = Color.FromArgb(225, 220, 60, 0);
    
                ApplicationBarIconButton button1 = new ApplicationBarIconButton();
                button1.IconUri = new Uri("/Images/favs.addto.png", UriKind.Relative);
                button1.Text = "Add as Favorite";
                ApplicationBar.Buttons.Add(button1);
                button1.Click += new EventHandler(button1_Click);
    
                ApplicationBarIconButton button2 = new ApplicationBarIconButton();
                button2.IconUri = new Uri("/Images/feature.email.png", UriKind.Relative);
                button2.Text = "Email";
                ApplicationBar.Buttons.Add(button2);
                button2.Click += new EventHandler(button2_Click);
    
                ApplicationBarIconButton button3 = new ApplicationBarIconButton();
                button3.IconUri = new Uri("/Images/homeicon.png", UriKind.Relative);
                button3.Text = "Home";
                ApplicationBar.Buttons.Add(button3);
                button3.Click += new EventHandler(button3_Click);
          
            }
    
    
    
            private void link1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.microsoft.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
    
    
            }
    
            private void link2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.intel.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
    
            }
    
            private void link3_Tap(object sender, System.Windows.Input.GestureEventArgs e)
            {
    
                string site = "http://www.ibm.com";
                webBrowser1.Visibility = System.Windows.Visibility.Visible;
                webBrowser1.Source = new Uri(site, UriKind.Absolute);
    
            }
    
            
            protected override void OnBackKeyPress(CancelEventArgs e)
            {
                if (webBrowser1.CanGoBack)
                {
                    e.Cancel = true;
                    webBrowser1.GoBack();
                }
                else
                {
                    base.OnBackKeyPress(e);
                }
            }
    
    
    
            
            private void button1_Click(object sender, EventArgs e)
            {
                
                MessageBox.Show("Button 1 works!");
              //  MessageBox.Show(site);
               //Do work for your application here.
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
            
                
                MessageBox.Show("Button 2 works!");
             }
    
    
            private void button3_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Button 3 works!");
            }
    
    
    
      
    
        }
    }

    Declare variables immediately after these lines:

    public partial classtest : PhoneApplicationPage
       
    {
        // Class member variables go below this comment.

    Then they become class-wide members and can be accessed by any method in the class and have a lifetime that is the same as the page object's lifetime.

    Hugh


    Hugh Moran - http://www.morantex.com




    • Edited by Morantex Monday, September 30, 2013 3:54 PM
    • Proposed as answer by Oleg Kurzov Monday, September 30, 2013 4:23 PM
    Monday, September 30, 2013 3:52 PM
  • Monday, September 30, 2013 4:24 PM
  • Thanks Hugh. So if I define "site" as a variable in the class test, it does show up for all methods.

    The issue I am trying to resolve is that I want to pass a different URL to this variable depending on what the user clicks on XAML page and I am planning to have as many as 20 links on the production XAML pages.

    How do I pass a different URL every time when the user clicks on the link and then have it assign to the site variable? 

    cheers,

    mujno

        


    mujno

    Monday, September 30, 2013 6:53 PM
  • I don't understand your response. Please elaborate.

    thanks


    mujno

    Monday, September 30, 2013 7:27 PM