如何使事件处理程序中的变量可用于同一页面上的其他方法? [英] how to make variables in event handlers available to other methods on the same page?

查看:66
本文介绍了如何使事件处理程序中的变量可用于同一页面上的其他方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和WP开发的新手。我有一个简单的页面,其中有三个链接可以访问3个不同的站点。我还有一个应用栏,我将提供不同的操作,例如电子邮件共享和保存到隔离存储。我通过在事件处理程序本身中声明一个变量,使用浏览器控件将
转到三个不同的站点。我遇到问题,我的应用栏事件处理程序可以使用这些变量。如何在同一页面上将其提供给其他方法?

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?

这是我的XAML页面的一个示例:

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>

这是我的c#页面:

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!");
        }



  

    }
}

推荐答案


我是C#和WP开发的新手。我有一个简单的页面,其中有三个链接可以访问3个不同的站点。我还有一个应用栏,我将提供不同的操作,例如电子邮件共享和保存到隔离存储。我通过在事件处理程序本身中声明一个变量,使用浏览器控件将
转到三个不同的站点。我遇到问题,我的应用栏事件处理程序可以使用这些变量。如何在同一页面上将其提供给其他方法?

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?

这是我的XAML页面的一个示例:

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>

这是我的c#页面:

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
部分
class 测试

PhoneApplicationPage

   
{

   ;&NBSP; //类成员变量低于此注释。

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.


这篇关于如何使事件处理程序中的变量可用于同一页面上的其他方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆