添加在侧边栏的Windows Phone 8.1应用程序 [英] Add a sidebar in Windows Phone 8.1 application

查看:151
本文介绍了添加在侧边栏的Windows Phone 8.1应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的Windows Phone 8.1应用程序,我希望像Android应用侧边栏。我该怎么办呢?

I'm building Windows Phone 8.1 application and I want a sidebar like in android apps. How can I do it?

我试过的此内容例子,但不编译和操纵事件的不火。

I tried this example, but doesn't compile and manipulation event's do not fire.

推荐答案

下面是速度的处理很简单的例子。

Here is very simple example with velocity handling.

请一定要设置网格的操作属性到所有

Make sure to set grid's Manipulation property to All.

深橙红更改网格的背景透明

在这里输入的形象描述

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();
    }

这篇关于添加在侧边栏的Windows Phone 8.1应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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