プログラミングとかブログ

Unity/C#/SRPGStudio/RPGツクールMVの情報とかその他気になったことを調べて書きます。

【C#】Marginをアニメーションさせる

ビハインドでMarginを一定間隔で毎回生成することで移動させます。
f:id:shirakamisauto:20160718175005g:plain

//XAML
<Page
    x:Class="UWPtest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:UWPtest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock x:Name="textBlock" Margin="0,70,0,0" Text="タイトル"/>
        <Button x:Name="button" Content="Button" Margin="0,33,0,295" Click="button_Click"/>
    </Grid>
</Page>


//コードビハインド
public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void button_Click(object sender, RoutedEventArgs e)
        {
            button.IsEnabled = false;

            int top = 70;
            while (top < 250)
            {
                top += 3;
                textBlock.Margin = new Thickness(0, top, 0, 0);
                await System.Threading.Tasks.Task.Delay(
                    TimeSpan.FromMilliseconds(33));//30fps
            }

            button.IsEnabled = true;
        }
    }