如何使ScrollViewer在Windows Phone中正常工作

时间:2012-08-19 17:02:35

标签: c# .net windows-phone-7

我在我的应用程序页面上添加了一个ScrollViewer对象,并且我在ScrollViewer对象上添加了许多控件对象,但使用该应用程序的最终用户无法查看所有元素,因为Scroll滚动不够低,而且页面在用户有机会输入任何信息之前,它会一直回到原来的位置。

这是我的XAMLCode:

<phone:PhoneApplicationPage 
    x:Class="WinHomeWork2.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"
    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" Loaded="PhoneApplicationPage_Loaded">

    <!--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="VOLKOV LTD" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Agent_App" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <ScrollViewer
         Name="mainScrollViewer"
         Margin="0,175,0,0"
         VerticalScrollBarVisibility="Visible" AllowDrop="False" ManipulationMode="Control">

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="100"/>
                    <RowDefinition Height="300*"/>
                </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100*"/>
                <ColumnDefinition Width="20"/>
                <ColumnDefinition Width="200*"/>
            </Grid.ColumnDefinitions>


            <TextBox 
                Name="AgentName" 
                Height="80" 
                Grid.Row="0" 
                Grid.Column="3"
                />

            <TextBlock 
                Name="AgentNameTextBlock"
                FontSize="25"
                Text="Agent Name" 
                Grid.Row="0" 
                Grid.Column="0" 
                Grid.ColumnSpan="1" 
                />

            <TextBlock
                Name="PasswordTextBlock"
                Grid.Row="1"
                Grid.Column="0"
                Text="Password"
                FontSize="25"
                />

            <PasswordBox
                Name="Agent_Password"
                Height="80"
                Grid.Row="1"
                Grid.Column="3"
                />

            <CheckBox
                Name="myCheckbox"
                Grid.Row="2"
                Grid.Column="0"
                />

            <TextBlock 
                Name="checkboxTextBlock"
                Width="350"
                Height="30"
                Grid.Row="2"
                Grid.Column="0"
                Grid.ColumnSpan="3"
                Text="Was The Agent Undercover?"
                FontSize="25"
                />


      <!-- Insert Radiobuttons on the next 3 rows-->     

            <RadioButton
                Name="radioButton_CIA"
                GroupName="Agency"
                Grid.Row="3"
                Grid.Column="3"
                />

            <TextBlock Name="ciaTextBlock" Text="CIA" Grid.Column="3" Grid.Row="3" FontSize="25" Height="30" Width="160" Tap="ciaTextBlock_Tap" />

            <RadioButton
                Name="radioButton_FBI"
                GroupName="Agency"
                Grid.Row="4"
                Grid.Column="3"
                />

            <TextBlock Name="fbiTextBlock" Text="FBI" Grid.Row="4" Grid.Column="3" FontSize="25" Height="30" Width="160" Tap="fbiTextBlock_Tap" />

            <RadioButton
                Name="radioButton_MI6"
                GroupName="Agency"
                Grid.Row="5"
                Grid.Column="3"
                />

            <TextBlock Name="mi6TextBlock" Text="MI6" Grid.Row="5" Grid.Column="3" FontSize="25" Height="30" Width="160" Tap="mi6TextBlock_Tap" />

                <Button Name="enterBotton" Content="Go" Grid.Row="7" Grid.Column="3" FontSize="25" Click="enterBotton_Click"/>

                <ListBox  Name="myList" Grid.Row="6" Grid.Column="0" >
                <ListBoxItem Name="optionOne" Content="Find"/>
                <ListBoxItem Name="optionTwo" Content="Store"/>
                <ListBoxItem Name="optionThree" Content="Flip"/>
                </ListBox>

            </Grid>
        </ScrollViewer>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

请帮助我!

以下是Code Behind:

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 WinHomeWork2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void enterBotton_Click(object sender, RoutedEventArgs e)
        {
            var Agent = new Agent()
            {
                AgentName = AgentName.Text,
                Password = Agent_Password.Password,
                RecordTime = DateTime.Now,
                Undercover = myCheckbox.IsChecked,
                Agency = getAgency()
            };
        }

       private string getAgency()
       {

           return (bool)radioButton_CIA.IsChecked? "CIA" : (bool)radioButton_FBI.IsChecked ? "FBI" : (bool)radioButton_MI6.IsChecked? "MI6" : null;
       }

       private void ciaTextBlock_Tap(object sender, GestureEventArgs e)
       {
           radioButton_CIA.IsChecked = true;
       }

       private void fbiTextBlock_Tap(object sender, GestureEventArgs e)
       {
           radioButton_FBI.IsChecked = true;
       }

       private void mi6TextBlock_Tap(object sender, GestureEventArgs e)
       {
           radioButton_MI6.IsChecked = true;
       }

       private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
       {
           AgentName.Focus();
       }




    }

    //Agent Class
    public class Agent
    {
        public Agent() { }
        public string AgentName { get; set; }
        public string Password { get; set; }
        public DateTime RecordTime { get; set; }
        public bool? Undercover { get; set; }
        public string Agency { get; set; }
    }
}

2 个答案:

答案 0 :(得分:8)

定义Scrollviewer的高度以使其适合屏幕。

答案 1 :(得分:3)

我猜问题就在于这两个陈述。您忘记更改Grid.Row值。请进行更改,然后重试

    <ScrollViewer Grid.Row="1"
     Name="mainScrollViewer"
     Margin="0,175,0,0"
     VerticalScrollBarVisibility="Visible" AllowDrop="False" ManipulationMode="Control">

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Margin="12,0,12,0">