Win8.1 App 在xaml中实现颜色选择器的功能

1 篇文章 0 订阅


做一个更换背景颜色的功能, 但是感觉xaml工具箱里面控件种类不如MFC里面的多, 借鉴别人的博客, 实现了颜色选择器的功能

本来计划做win8 app 但是还是做成了win8.1 的,  因为win8不支持flyout

大功告成 !


步骤1 在appbar上面添加一个按钮,  按钮内设置flyou控件 

在内添加combobox控件  combobox添加响应函数 background_color_combo_SelectionChanged , 并且绑定数据

    <Page.BottomAppBar>
        <AppBar x:Name="bottomAppBar" 
                Opened="BottomAppBar_Opened" Closed="BottomAppBar_Closed" IsTapEnabled="False"
                IsDoubleTapEnabled="False" IsHoldingEnabled="False" 
                IsRightTapEnabled="False" IsTabStop="False" IsSticky="False"  BorderBrush="#00348DFD">
            <AppBar.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Offset="0" Color="Transparent"/>
                    <GradientStop Color="#FF348DFD" Offset="1"/>
                    <GradientStop Color="#FF348DFD" Offset="0.068"/>
                </LinearGradientBrush>
            </AppBar.Background>
            <Grid>
                     <Button HorizontalAlignment="Right" Name="backgroundbtn"
                            Style="{StaticResource FontColorAppBarButtonStyle}">
                        <Button.Flyout>
                            <Flyout>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>

                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="0">

                                        <TextBlock  HorizontalAlignment="Left" TextWrapping="NoWrap"
                                            Text="背景颜色" VerticalAlignment="Center"  FontSize="15"/>
                                        <ComboBox x:Name="background_color_combo"  Width="200" Height="30" 
                                          ItemsSource="{Binding Colors}"
                                          SelectedItem="{Binding SelectedColorName, Mode=TwoWay}" 
                                          SelectionChanged="background_color_combo_SelectionChanged"
                                          Padding="8,0" Margin="10">
                                            <ComboBox.ItemTemplate>
                                                <DataTemplate>
                                                    <Grid>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="Auto"/>
                                                            <ColumnDefinition Width="*"/>
                                                        </Grid.ColumnDefinitions>
                                                        <Rectangle Width="20" Height="20" Fill="{Binding Name}" Margin="5,0"/>
                                                        <TextBlock Name="ColorNam"  Grid.Column="1" Margin="10,0,0,0" Text="{Binding Name}" Foreground="Black"/>
                                                    </Grid>
                                                </DataTemplate>
                                            </ComboBox.ItemTemplate>
                                        </ComboBox>
                                    </StackPanel>
                                </Grid>
                            </Flyout>
                        </Button.Flyout>
                    </Button>
                </StackPanel>
            </Grid>
        </AppBar>
    </Page.BottomAppBar>


步骤2 在loadstate中对combo控件进行初始化

            ///设置颜色选择器
            var colors = typeof(Colors).GetTypeInfo().DeclaredProperties;
            foreach (var item in colors)
            {
                background_color_combo.Items.Add(item);
                if (((Color)item.GetValue(null)).ToString() == 
                    ((SolidColorBrush)grid.Background).Color.ToString())
                {
                    background_color_combo.SelectedItem = item;
                }
            }

步骤 3 完成响应函数

        private void background_color_combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var combo = (ComboBox)sender;
            if (combo.SelectedIndex != -1)
            {
                var pi = combo.SelectedItem as PropertyInfo;
                var col = new SolidColorBrush((Color)pi.GetValue(null));
                combo.BorderBrush = col;
                grid.Background = col;
            }

        }

步骤4 保存到本地数据中

 Windows.Storage.ApplicationDataContainer localSettings
               = Windows.Storage.ApplicationData.Current.LocalSettings;

            //背景颜色
            var c = ((SolidColorBrush)grid.Background).Color;
            var backgroundColor = new Windows.Storage.ApplicationDataCompositeValue();
            backgroundColor["r"] = c.R;
            backgroundColor["g"] = c.G;
            backgroundColor["b"] = c.B;
            localSettings.Values["BackgroundColor"] = backgroundColor;



步骤5  在loadstate中载入保存的背景颜色

            //加载 背景颜色 和 透明度
            Windows.Storage.ApplicationDataCompositeValue backgroundColor =
            (Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["BackgroundColor"];
            if (backgroundColor == null)
            {
                //载入默认值
                backgroundColor = new Windows.Storage.ApplicationDataCompositeValue();
                backgroundColor["r"] = (Byte)255;
                backgroundColor["g"] = (Byte)255;
                backgroundColor["b"] = (Byte)255;
                localSettings.Values["BackgroundColor"] = backgroundColor;
            }
            Byte R = (Byte)backgroundColor["r"];
            Byte G = (Byte)backgroundColor["g"];
            Byte B = (Byte)backgroundColor["b"];
            grid.Background = new SolidColorBrush(Color.FromArgb(255, (Byte)R, (Byte)G, (Byte)B));


大功告成 ~ 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值