【问题标题】:Add a sidebar in Windows Phone 8.1 application在 Windows Phone 8.1 应用程序中添加侧边栏
【发布时间】:2015-02-02 00:19:00
【问题描述】:

我正在构建 Windows Phone 8.1 应用程序,我想要一个类似于 android 应用程序的侧边栏。我该怎么做?

我尝试了this 示例,但没有编译并且操作事件不会触发。

【问题讨论】:

    标签: c# windows-phone-8.1 win-universal-app


    【解决方案1】:

    这是一个非常简单的速度处理示例。

    确保将网格的Manipulation 属性设置为All

    将网格的背景从 DarkSalmon 更改为 Transparent

    XAML

    <Canvas Name="RootCanvas">
        <Canvas.Resources>
            <Storyboard x:Name="MoveAnimation">
                <DoubleAnimation Duration="0:0:0.2" To="0" Storyboard.TargetProperty="(Canvas.Left)" Storyboard.TargetName="Sidebar" d:IsOptimized="True" />
            </Storyboard>
        </Canvas.Resources>
    
        <Grid Name="Sidebar" Background="DarkSalmon" Width="360" Height="640" Canvas.Left="-340" ManipulationDelta="Sidebar_ManipulationDelta" ManipulationMode="All" ManipulationCompleted="Sidebar_ManipulationCompleted">
            <Grid Background="DarkSlateBlue" Margin="0,0,20,0">
                <StackPanel Orientation="Vertical">
                    <Button Content="Mailbox" />
                    <Button Content="Calendar" />
                    <Button Content="Tasks" />
                    <Button Content="Documents" />
                </StackPanel>
            </Grid>
        </Grid>
    </Canvas>
    

    代码隐藏

    private bool _triggerCompleted;
    
        private const double SideMenuCollapsedLeft = -340;
        private const double SideMenuExpandedLeft = 0;
    
        private void Sidebar_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            _triggerCompleted = true;
    
            double finalLeft = Canvas.GetLeft(Sidebar) + e.Delta.Translation.X;
            if (finalLeft < -340 || finalLeft > 0)
                return;
    
            Canvas.SetLeft(Sidebar, finalLeft);
    
            if (e.IsInertial && e.Velocities.Linear.X > 1)
            {
                _triggerCompleted = false;
                e.Complete();
                MoveLeft(SideMenuExpandedLeft);
            }
    
            if (e.IsInertial && e.Velocities.Linear.X < -1)
            {
                _triggerCompleted = false;
                e.Complete();
                MoveLeft(SideMenuCollapsedLeft);
            }
    
            if (e.IsInertial && Math.Abs(e.Velocities.Linear.X) <= 1)
                e.Complete();
        }
    
        private void Sidebar_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            if (_triggerCompleted == false)
                return;
    
            double finalLeft = Canvas.GetLeft(Sidebar);
    
            if (finalLeft > -170)
                MoveLeft(SideMenuExpandedLeft);
            else
                MoveLeft(SideMenuCollapsedLeft);
        }
    
        private void MoveLeft(double left)
        {
            double finalLeft = Canvas.GetLeft(Sidebar);
    
            Storyboard moveAnivation = ((Storyboard)RootCanvas.Resources["MoveAnimation"]);
            DoubleAnimation direction = ((DoubleAnimation)((Storyboard)RootCanvas.Resources["MoveAnimation"]).Children[0]);
    
            direction.From = finalLeft;
    
            moveAnivation.SkipToFill();
    
            direction.To = left;
    
            moveAnivation.Begin();
        }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个解决方案:

      http://slideview.codeplex.com/

      希望它会有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        相关资源
        最近更新 更多