0

这是我第一次接触编程。从来没有任何经验,所以请忍受我对这个主题的微薄知识。我正在为将连接到用户定义的 IP 安全摄像头的 Windows Phone 应用程序。我收到这些错误:

Error       1      The name 'box1' does not exist in the current contex

最后一个错误发生在所有 box1/2/3/4

这是我的代码(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;

namespace SecurityCamera
{

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage();
        {
             InitializeComponent();
             defaultPicker.ItemsSource = new List<string>() { {box1.Text}, {box2.Text}, {box3.Text}, {box4.Text} };
        }
            public string Name
            {
                get;
                set;
            }



       /// <summary>
       /// Go button clicked event handler
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button1_Click(object sender, RoutedEventArgs e)
       {
           NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
       }

    }

XAML:

 x:Class="SecurityCamera.MainPage"
    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"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Name="ListPickerItemTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" Margin="10 0 0 0"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Name="ListPickerFullModeItemTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" Margin="10 0 0 0"/>
            </StackPanel>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Security Camera Access" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->

        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel Margin="0,-28,96,616">
                <toolkit:ListPicker ExpansionMode="FullScreenOnly"  Header="Specify IP:" Grid.Row="1" x:Name="defaultPicker" Height="687"/>


            </StackPanel>
            <Button Content="New..." Height="70" HorizontalAlignment="Left" Margin="360,10,0,0" x:Name="button1" VerticalAlignment="Top" Width="96" Click="button1_Click" FontSize="16" RenderTransformOrigin="0.612,1.036" />
            <phone:WebBrowser HorizontalAlignment="Left" Height="595" Margin="0,91,0,0" VerticalAlignment="Top" Width="446" IsScriptEnabled="True"/>
        </Grid>

    </Grid>


</phone:PhoneApplicationPage>

Pg2 XAML:

x:Class="SecurityCamera.Page1"
    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="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="Security Camera Access" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="Add New IP's" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox x:Name="box1" HorizontalAlignment="Left" Height="70" Margin="31,5,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="400"/>
            <TextBox x:Name="box2"  HorizontalAlignment="Left" Height="70" Margin="31,75,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="400"/>
            <TextBox x:Name="Box3" HorizontalAlignment="Left" Height="70" Margin="31,150,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="400"/>
            <TextBox x:Name="box4" HorizontalAlignment="Left" Height="70" Margin="31,225,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="400"/>
            <TextBlock HorizontalAlignment="Left" Height="70" Margin="61,300,0,0" TextWrapping="Wrap" Text="IMPORTANT: Include http:// before all IP addresses!" VerticalAlignment="Top" Width="328"/>
            <Button Content="Done" HorizontalAlignment="Left" Height="80" Margin="84,406,0,0" VerticalAlignment="Top" Width="305" Click="Button_Click_1"/>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

我觉得这很奇怪,因为我的代码之前没有出现这些错误,而且我没有碰过任何东西(我知道)。

4

2 回答 2

2

问题 #1 是这样的:

// Constructor
    public MainPage();
    {
         InitializeComponent();
         defaultPicker.ItemsSource = new List<string>() { {box1.Text}, {box2.Text}, {box3.Text}, {box4.Text} };
    }

构造函数语法在名称后没有分号,应该是这样的:

// Constructor
    public MainPage()
    {
         InitializeComponent();
         defaultPicker.ItemsSource = new List<string>() { {box1.Text}, {box2.Text}, {box3.Text}, {box4.Text} };
    }
于 2013-07-08T01:26:50.803 回答
1

如果这是您的整个代码,那么您将缺少}关闭namespace范围的最后一个大括号,并且您的构造函数后面不应有分号

public MainPage()
{
    InitializeComponent();
    defaultPicker.ItemsSource = new List<string>() { {box1.Text}, {box2.Text}, {box3.Text}, {box4.Text} };
}

根据编辑的问题更新

您收到该错误是因为box1Page1课堂上,但您试图从MainPage. 您必须使用背后的代码Page1或学习MVVM 模式,以便将逻辑与 UI 分离。

于 2013-07-08T01:25:29.177 回答