消除XAML代码重复

时间:2012-07-05 08:39:14

标签: c# silverlight windows-phone-7 xaml optimization

我希望在我的Windows移动应用程序中有转换效果,并且我能够使用silverlight工具包的转换效果实现它。但问题是我最终复制了所有XAML页面中的确切代码。

我已经提供了位于页面底部的MainPage.xaml和重复工具包代码的摘录。有没有办法优化这个(在一个地方并使用它)?

在Ruby on Rails中,我只想为该代码创建一个部分。这里有类似的东西吗?

<phone:PhoneApplicationPage
    xmlns:toolkit="xyz"
    xmlns="xmlnamespace_value">

    <!-- PAGE DESIGN START -->
    ...
    ...
    ...
    ...
    <!-- PAGE DESIGN END -->

    <!-- REPEATING CODE START -->
    <toolkit:TransitionService.NavigationInTransition>
        <toolkit:NavigationInTransition>
            <toolkit:NavigationInTransition.Backward>
                <toolkit:TurnstileTransition Mode="BackwardIn"/>
            </toolkit:NavigationInTransition.Backward>
            <toolkit:NavigationInTransition.Forward>
                <toolkit:TurnstileTransition Mode="ForwardIn"/>
            </toolkit:NavigationInTransition.Forward>
        </toolkit:NavigationInTransition>
    </toolkit:TransitionService.NavigationInTransition>
    <!-- REPEATING CODE END -->
</phone:PhoneApplicationPage>

2 个答案:

答案 0 :(得分:1)

也许您可以将重复代码分成UserControl

答案 1 :(得分:1)

使用Style设置页面效果。

<强>更新

  1. Styles.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style x:Key="MyPageStyle" TargetType="Page">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect />
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="AliceBlue" />
        </Style>
    </ResourceDictionary>
    
  2. 的App.xaml

    <Application x:Class="WpfBrowserApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Page1.xaml">
        <Application.Resources>
            <ResourceDictionary Source="Styles.xaml" />
        </Application.Resources>
    </Application>
    
  3. 的Page1.xaml

    <Page x:Class="WpfBrowserApplication1.Page1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          Title="Page1"
          d:DesignHeight="300"
          d:DesignWidth="300"
          Style="{StaticResource MyPageStyle}" // Take a look at this line
          mc:Ignorable="d">
        <Grid />
    </Page>
    
相关问题