【问题标题】:How to construct a settings page similar to the stock one in Windows Phone 7?如何构建一个类似于 Windows Phone 7 中的股票的设置页面?
【发布时间】:2012-01-19 21:48:01
【问题描述】:

当我启动 Windows Phone 设置应用程序时,显示的是一个枢轴控件,每个页面上都有一堆项目。例如,第一项是:

THEME
blue

创建这些项目的标准方法是什么?我希望它们具有相同的字体样式和外观。是否有任何控件可以代表上面的项目?

谢谢!

【问题讨论】:

  • 我在答案中添加了一个简单的代码示例来清除问题.. :)

标签: windows-phone-7.1 windows-phone-7


【解决方案1】:

它是一个 ListBox 控件和每个项目的 DataTemplate。该模板定义了两个 TextBox 控件,一个用于“标题”,一个用于“描述/值”。您可以为每个 TextBox 设置样式。

编辑:这是一个示例代码

<ListBox x:Name="YourListBox" Margin="0" ItemsSource="{Binding YourItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding ItemTitle}" TextWrapping="Wrap" Style="{StaticResource PhoneTextTitle2Style}"/>
                <TextBlock Text="{Binding ItemValue}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

【讨论】:

  • 谢谢阿巴斯。它为我指明了正确的方向,但您指定的样式与“设置”应用程序使用的样式不同。反正我给了你一些声望点:)
【解决方案2】:

在逐个像素地玩了 2 个小时并使用绘图程序将屏幕截图与真实的东西并排比较之后(一定有更好的方法!),我找到了以下解决方案。

这是完全复制的设置页面。我创建了一个自定义用户控件,以便将项目添加到 xaml 就像这样简单:

<MyApp:SettingsItem Label="theme" Sub="blue"/>

代码如下:

SettingsItem.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyApp
{
  // This custom control class is to show a standard item in a settings page.

  public partial class SettingsItem : UserControl
  {
    private const string TAG = "SettingsItem";

    public SettingsItem()
    {
      // Required to initialize variables
      InitializeComponent();
    }

    public static readonly DependencyProperty LabelProperty =
        DependencyProperty.Register(
            "Label",
            typeof(string),
            typeof(SettingsItem),
            new PropertyMetadata(new PropertyChangedCallback
              (OnLabelChanged)));

    public string Label
    {
      get
      {
        return (string)GetValue(LabelProperty);
      }
      set
      {
        SetValue(LabelProperty, value);
      }
    }

    private static void OnLabelChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
    {
      SettingsItem item = (SettingsItem)d;
      string newValue = (string)e.NewValue;
      item.m_label.Text = newValue.ToLower();
    }

    public static readonly DependencyProperty SubProperty =
        DependencyProperty.Register(
            "Sub",
            typeof(string),
            typeof(SettingsItem),
            new PropertyMetadata(new PropertyChangedCallback
              (OnSubChanged)));

    public string Sub
    {
      get
      {
        return (string)GetValue(SubProperty);
      }
      set
      {
        SetValue(SubProperty, value);
      }
    }

    private static void OnSubChanged(DependencyObject d,
      DependencyPropertyChangedEventArgs e)
    {
      SettingsItem item = (SettingsItem)d;
      string newValue = (string)e.NewValue;
      item.m_sub.Text = newValue;
    }
  }
}

SettingsItem.xaml

<UserControl
  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"
  mc:Ignorable="d"
  x:Class="MyApp.SettingsItem"
  d:DesignWidth="428" d:DesignHeight="0">

  <Grid x:Name="LayoutRoot" Background="Transparent">

    <StackPanel 
      Orientation="Vertical"
      >

      <TextBlock 
        x:Name="m_label"
        Text="label" 
        Style="{StaticResource PhoneTextExtraLargeStyle}"
        Margin="0, 18, 0, 0"
        />

      <TextBlock 
        x:Name="m_sub"
        Text="Sub" 
        Style="{StaticResource PhoneTextSubtleStyle}"
        TextWrapping="Wrap"
        Margin="0, -6, 0, 0"
        />

    </StackPanel>
  </Grid>
</UserControl>

这是页面 xaml:

<phone:PhoneApplicationPage 
    x:Class="MyApp.SettingsPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"

    xmlns:MyApp="clr-namespace:MyApp;assembly=MyApp" 

    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">

        <!--Pivot Control-->
        <controls:Pivot Title="MY APP" SelectedIndex="0">

            <controls:PivotItem 
                Name="PIVOT_GENERAL"
                Margin="0" 
                Header="settings">
                <Grid Margin="26,9,0,0">
                    <StackPanel>

                        <MyApp:SettingsItem
                            Label="theme"
                            Sub="blue"
                            />

                        <MyApp:SettingsItem
                            Label="date+time"
                            Sub="UTC-08 Pacific Time (US + Canada)"
                            />

                        <MyApp:SettingsItem
                            Label="region+language"
                            Sub="United States"
                            />

                    </StackPanel>

                </Grid>
            </controls:PivotItem>

        </controls:Pivot>
    </Grid>

</phone:PhoneApplicationPage>

【讨论】:

    【解决方案3】:

    我的建议:

    <Button>
    <Button.Template>
        <ControlTemplate>
            <StackPanel>
                <TextBlock Text="Label" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="0 18 0 0"/>
                <TextBlock Text="Sub" Style="{StaticResource PhoneTextSubtleStyle}" Margin="0 -6 0 0"/>
            </StackPanel>
        </ControlTemplate>
    </Button.Template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多