WPF Transitionals组件使用笔记

<UserControl x:Class="CMApplication1.Views.TransitionalsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:trans="clr-namespace:Transitionals;assembly=Transitionals"
             xmlns:transc="clr-namespace:Transitionals.Controls;assembly=Transitionals"
             xmlns:transt="clr-namespace:Transitionals.Transitions;assembly=Transitionals"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cal="http://www.caliburnproject.org"
             mc:Ignorable="d"  Width="800" Height="600"  cal:Message.Attach="[Loaded] = [Load($source)]"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Background>
        <ImageBrush ImageSource="/CMApplication1;component/Images/TransitionalsViewBG.jpg" />
    </UserControl.Background>
    <Grid>
        <StackPanel>
            <Button  FontSize="16" Content=" 测试动画">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <cal:ActionMessage MethodName="Test">
                            <cal:Parameter Value="{Binding ElementName=TE}" />
                        </cal:ActionMessage>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
            <ComboBox FontSize="16" x:Name="Transitions" DisplayMemberPath="Name" SelectedValuePath="FullName" cal:Message.Attach="[SelectionChanged] = [TransitionSelected($source)]" />
            <Button Content="自动执行动画测试">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <cal:ActionMessage MethodName="AutoTest">
                            <cal:Parameter Value="{Binding ElementName=TE}" />
                        </cal:ActionMessage>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
            <transc:TransitionElement x:Name="TE" Transition="{Binding XTransition}">
            </transc:TransitionElement>
        </StackPanel>
                  
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using Caliburn.Micro;
using Transitionals.Controls;
using Transitionals.Transitions;
using CMApplication1.Views;
using CMApplication1.Models;
namespace CMApplication1.ViewModels
{
    [Description("Transitionals Study")]
    [Export(typeof(TransitionalsViewModel))]
    public class TransitionalsViewModel : PropertyChangedBase, IShell
    {
        System.Windows.Controls.Image img1 = new System.Windows.Controls.Image();
        System.Windows.Controls.Image img2 = new System.Windows.Controls.Image();
        System.Windows.Controls.Image img3 = new System.Windows.Controls.Image();
        System.Windows.Controls.Image img4 = new System.Windows.Controls.Image();
        System.Windows.Controls.Image img5 = new System.Windows.Controls.Image();
        public Transitionals.Transition XTransition { get; set; }
        public BindableCollection<TransitionalsModel> Transitions { get; set; }
        public void Load(TransitionalsView view)
        {
            var assembly = typeof(Transitionals.Transition).Assembly;
            Transitions = new BindableCollection<TransitionalsModel>(assembly.GetTypes().OrderBy(_ => _.Name).Where(_ => _.Name.EndsWith("Transition") && _.GetConstructors().Count() > 0).Select(_ => new TransitionalsModel { Name = _.Name, FullName = _.FullName }));
            this.NotifyOfPropertyChange(() => Transitions);
            img1.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/BG.jpg", UriKind.RelativeOrAbsolute));
            img2.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/ShellViewBG.jpg", UriKind.RelativeOrAbsolute));
            img3.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/CC.jpg", UriKind.RelativeOrAbsolute));
            img4.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/DD.jpg", UriKind.RelativeOrAbsolute));
            img5.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("/Images/EE.jpg", UriKind.RelativeOrAbsolute));
        }
        public void TransitionSelected(System.Windows.Controls.ComboBox comb)
        {
            var assembly = typeof(Transitionals.Transition).Assembly;
            try
            {
                XTransition = (Transitionals.Transition)assembly.CreateInstance(comb.SelectedValue.ToString());
                this.NotifyOfPropertyChange(() => XTransition);
                flag = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                flag = false;
            }
            this.NotifyOfPropertyChange(() => CanTest);
        }
        private bool flag = false;
        public bool CanTest
        {
            get {
                return flag;
            }
        }
        private int i = 0;
        public void Test(TransitionElement te)
        {
            if (te != null)
            {
                if (i % 2 == 0)
                {
                    te.Content = img1;
                }
                else
                {
                    te.Content = img2;
                }
                i++;
            }
        }
        bool canGoFlag = false;
        public bool CanGo
        {
            get {
                return canGoFlag;
            }
        }
        public void AutoTest(TransitionElement te)
        {
            var assembly = typeof(Transitionals.Transition).Assembly;
            Circle(assembly, te);
               
            te.TransitionEnded += (sender, e) => {
                Circle(assembly, te);
            };
        }
        public void Circle(System.Reflection.Assembly assembly, TransitionElement te)
        {
            if (i == Transitions.Count - 1)
            {
                i = 0;
            }
            var trans = (Transitionals.Transition)assembly.CreateInstance(Transitions[i].FullName);
            var duration = System.Windows.Duration.Automatic;
            if (duration.HasTimeSpan)
            {
                trans.Duration = duration;
            }
            XTransition = trans;
            this.NotifyOfPropertyChange(() => XTransition);
            if (te != null)
            {
               if (i % 3 == 1)
                {
                    te.Content = img3;
                }
                else if (i % 3 == 2)
                {
                    te.Content = img4;
                }
                else
                {
                    te.Content = img5;
                }
                i++;
            }
        }
    }
}


知识共享许可协议
《WPF Transitionals组件使用笔记》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     149.12ms