2

C# を使用して、メトロ アプリのコンボボックスに色のリストを追加したいと考えています。次に、ユーザーはリストから特定の色を選択して背景を変更できます。

利用可能なライブラリは Windows.UI.Colors です。

シンプルなデスクトップ アプリでそれを実現するためのリンクを次に示します。 /

しかし、メトロ環境に移植することはできませんでした。

また、リスト項目としての色名と色自体の両方が大きなプラスになります。

MSDN の別のスレッド: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/880a3b5b-e287-4cdc-a1ab-d1cd4a19aedb/

4

2 に答える 2

3

このコードは私のために働きます:

var colorsTypeInfo = typeof(Colors).GetTypeInfo();
var properties = colorsTypeInfo.DeclaredProperties;
Dictionary<string, string> colours = new Dictionary<string, string>();
foreach (var dp in properties)
{
    colours.Add(dp.Name, dp.GetValue(typeof(Colors)).ToString());
}

次の参照を追加したことを確認してください。追加しないと機能しません。

using System.Reflection;
using Windows.UI;
于 2012-04-13T09:47:01.620 に答える
2
<ComboBox x:Name="cbColorNames" Grid.Row="1" Height="40"
      ItemsSource="{Binding Colors}"
      SelectedItem="{Binding SelectedColorName, Mode=TwoWay}">
<ComboBox.ItemTemplate>
    <DataTemplate>
        <Grid Background="Black">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Rectangle Width="35" Height="20" Fill="{Binding Name}" Margin="5,0"/>
            <TextBlock Grid.Column="1" Margin="10,0,0,0" Text="{Binding Name}" Foreground="White"/>
        </Grid>
    </DataTemplate>
</ComboBox.ItemTemplate>

これはxamlファイルです。

private static void LoadColors()
{
    var t = typeof(Colors);
    var ti = t.GetTypeInfo();
    var dp = ti.DeclaredProperties;
    colors = new List<PropertyInfo>();
    foreach (var item in dp)
    {
        colors.Add(item);
    }
}
private static List<PropertyInfo> colors;
public List<PropertyInfo> Colors
{
    get
    {
        if (colors == null)
            LoadColors();
        return colors;
    }
}

これはC#コードです。

サポートとヘルプをありがとうございました。

于 2012-04-13T09:45:23.917 に答える