出现在SIP键盘上设置垂直偏移

时间:2012-07-15 05:22:04

标签: c# silverlight windows-phone-7 xaml expression-blend

我的Windows Phone 7应用程序中有一些页面,里面有很多文本框。正如我们所知,Windows Phone 7的默认行为是,当我们专注于任何文本框时,SIP键盘会出现并向上滑动整个页面并在失去焦点时恢复到正常状态。 我想在我的应用程序中限制这个东西。正如我用Google搜索并找到解决方案一样,我们将所有控件放在scrollviewer中,并将焦点设置为textbox设置滚动查看器veritcal offset到该文本框的位置。

  1. 现在我很困惑如何告诉滚动查看器关于垂直偏移的控制位置。

  2. 并假设如果我可以设置该内容,我需要为我的文本框on GotFocusOn LostFocus调用两种方法。有没有其他方法可以通过交互行为和触发器来调用这个东西。

  3. 更新......

    <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="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="page name" 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">
                <Grid.RowDefinitions>
                    <RowDefinition Height="480" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <ScrollViewer x:Name="sc">
                    <StackPanel>
                        <Grid x:Name="Grid1" Grid.Row="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="80" />
                    <RowDefinition Height="80" />
                    <RowDefinition Height="80" />
                    <RowDefinition Height="80" />
                    <RowDefinition Height="80" />
                    <RowDefinition Height="80" />
                </Grid.RowDefinitions>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="0" x:Name="txtbox1"/>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="1" x:Name="txtbox2"/>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="2" x:Name="txtbox3"/>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="3" x:Name="txtbox4"/>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="4" x:Name="txtbox5"/>
                        <TextBox Width="460" AcceptsReturn="True" Grid.Row="5" x:Name="txtbox6" GotFocus="txtbox6_GotFocus"/>
                </Grid>
                    </StackPanel>
                </ScrollViewer>
                <Grid x:Name="Grid2" Grid.Row="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80" />
                    </Grid.RowDefinitions>
                    <Button x:Name="btnTest" Width="250" Content="Test" Grid.Row="0" />
                </Grid>
            </Grid>
        </Grid>
    

    AND CS C#....

    void ScrollToFocused(ScrollViewer scrollviewer)
    {
        FrameworkElement focusedElement = FocusManager.GetFocusedElement() as FrameworkElement;
        // verify focused control is a child of the ScrollViewer 
        if (scrollviewer != null && focusedElement != null && IsVisualChild(scrollviewer, focusedElement))
        {
            GeneralTransform focusedVisualTransform = focusedElement.TransformToVisual(scrollviewer);
            Rect rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(focusedElement.Margin.Left, focusedElement.Margin.Top), focusedElement.RenderSize));
            double newOffset = scrollviewer.VerticalOffset + (rectangle.Bottom - scrollviewer.ViewportHeight);
            scrollviewer.ScrollToVerticalOffset(newOffset);
        }
    }
    
    bool IsVisualChild(DependencyObject element, DependencyObject searchChildElement)
    {
        if (element == searchChildElement)
            return true;
        // recursively process nested children in the visual tree 
        int children = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < children; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(element, i);
            if (IsVisualChild(child, searchChildElement))
                return true;
        }
        return false;
    }
    
    private void txtbox6_GotFocus(object sender, RoutedEventArgs e)
    {
        txtbox6.Focus();
        ScrollToFocused(sc);
    } 
    

    但结果相同...... 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是因为你的网格高度有限。

更改   通过高度=“600”为网格提供一些高度。可能是600或超过600.it取决于您的页面。试试这个。我可以使用此解决方案解决我的问题