wpf应用程序中两个窗口之间的过渡效果

时间:2012-09-26 06:10:00

标签: c# .net wpf

我是WPF新手,正在创建一个简单的应用程序。基本上,该应用程序显示了一个人的年龄。我为此使用了两个窗口。第一个窗口将D.O.B作为输入,第二个弹出窗口显示其当前年龄。现在我想要的是将一些简单的过渡效果应用于显示年龄的弹出窗口。它可能是无关紧要的任何影响。下面是主窗口的c#代码。

namespace WpfApplication6
{

    public partial class MainWindow : Window
    {
        int year;
        public MainWindow()
        {
            InitializeComponent();
            string n = DateTime.Now.ToString();
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


        }

        private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string yearlist = comboBox3.SelectionBoxItem.ToString();
            string date = comboBox1.SelectionBoxItem.ToString();
            string month= comboBox2.SelectionBoxItem.ToString();
            if (date == "DD")
            { MessageBox.Show("Select date"); }
            else
            {
                if (month == "MM")
                { MessageBox.Show("Select month"); }
                else
                {

                    if (yearlist == "YYYY")
                    { MessageBox.Show("Select year"); }
                    else
                    {
                        System.DateTime dob = new System.DateTime(Convert.ToInt16(comboBox3.SelectionBoxItem), Convert.ToInt16(comboBox2.SelectionBoxItem), Convert.ToInt16(comboBox1.SelectionBoxItem), 12, 0, 0);

                        System.TimeSpan diff = DateTime.Now.Subtract(dob);
                        year = (Convert.ToInt32(diff.Days) / 365);
                        int days = (Convert.ToInt32(diff.Days) % 365);
                        string str = string.Format("Age is:{0} years and {1} days", year, days);
                        newwindow nw = new newwindow(str);
                        nw.Show();

                  }
                }
            }

        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}

现在弹出窗口的代码

namespace WpfApplication6
{

    public partial class newwindow : Window
    {

        public newwindow(string strmsg)
        {
            InitializeComponent();
            label1.Content = strmsg;
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        } 
    }
}

1 个答案:

答案 0 :(得分:0)

设置

otherWindow.Opacity=0; 
otherWindow.Show();

并应用从0到1的简单不透明度动画迭代。它将创建效果很好的淡入淡出。

DoubleAnimation da = new DoubleAnimation();
da.From = 1;
da.To = 0;
da.Duration = new Duration(TimeSpan.FromSeconds(2));
da.AutoReverse = true;
da.RepeatBehavior = RepeatBehavior.Forever;
// da.RepeatBehavior=new RepeatBehavior(3);
rectangle1.BeginAnimation(OpacityProperty, da);

请参阅http://tarundotnet.wordpress.com/2011/03/18/wpf-tutorial-change-opacity-using-animation/