【问题标题】:Eliminating XAML code duplication消除 XAML 代码重复
【发布时间】:2012-07-05 15:25:12
【问题描述】:

我想在我的 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>

【问题讨论】:

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


    【解决方案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>
      

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-06
        • 2018-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-02
        • 2013-01-30
        相关资源
        最近更新 更多