none
动态创建样式,动态生成style RRS feed

  • 问题

  • 项目中碰到一个问题 ,现在需要动态生成 style 而不是在xaml 文件中写死的,那就是要new style 实现类似以下的xaml代码

           <Style x:Key="TransitionPageStyle" TargetType="phone:PhoneApplicationPage">
                <Setter Property="toolkit:TransitionService.NavigationInTransition">
                    <Setter.Value>
                        <toolkit:NavigationInTransition>
                            <toolkit:NavigationInTransition.Backward>
                                <toolkit:TurnstileTransition Mode="BackwardIn"/>
                            </toolkit:NavigationInTransition.Backward>
                            <toolkit:NavigationInTransition.Forward>
                                <toolkit:TurnstileTransition Mode="ForwardIn"/>
                            </toolkit:NavigationInTransition.Forward>
                        </toolkit:NavigationInTransition>
                    </Setter.Value>
                </Setter>
                <Setter Property="toolkit:TransitionService.NavigationOutTransition">
                    <Setter.Value>
                        <toolkit:NavigationOutTransition>
                            <toolkit:NavigationOutTransition.Backward>
                                <toolkit:TurnstileTransition Mode="BackwardOut"/>
                            </toolkit:NavigationOutTransition.Backward>
                            <toolkit:NavigationOutTransition.Forward>
                                <toolkit:TurnstileTransition Mode="ForwardOut"/>
                            </toolkit:NavigationOutTransition.Forward>
                        </toolkit:NavigationOutTransition>
                    </Setter.Value>
                </Setter>
            </Style>
    
    


    我是这么写的 

    public static Style getStyle() 
            {
                Style style = new Style();
    
                style.TargetType = typeof(System.Windows.Controls.Page);
    
    
    
                var na1 =  new  NavigationInTransition( );
    
                var tu1 =  new  TurnstileTransition() ;
    
                var tu2 = new TurnstileTransition();
    
                tu1.Mode = TurnstileTransitionMode.BackwardIn;
    
                tu2.Mode = TurnstileTransitionMode.ForwardIn;
    
                na1.SetValue(NavigationInTransition.BackwardProperty, tu1);
                na1.SetValue(NavigationInTransition.BackwardProperty, tu2);
    
                style.Setters.Add(new Setter(TransitionService.NavigationInTransitionProperty ,na1 ));
    
    
                var na2 = new NavigationInTransition();
    
                var tu3 = new TurnstileTransition();
                var tu4 = new TurnstileTransition();
    
                tu3.Mode = TurnstileTransitionMode.BackwardOut;
                tu4.Mode = TurnstileTransitionMode.ForwardOut;
    
                var w = na2.GetValue(NavigationInTransition.BackwardProperty);
    
                na2.SetValue(NavigationInTransition.BackwardProperty, tu3);
                na2.SetValue(NavigationInTransition.BackwardProperty, tu4);
    
                style.Setters.Add(new Setter(TransitionService.NavigationInTransitionProperty, na2));
    
                System.Diagnostics.Debug.WriteLine(style.ToString());
    
    
                return style; 
            }
    

    但是没有效果 ,请问哪里错误?如果错了应该这么写?

    2012年2月5日 5:44

答案

全部回复