使用设置页面更改主页上的字体颜色

时间:2014-10-01 14:04:22

标签: c# windows-phone-8 fonts

我正在尝试完成一个应用程序,我还有一个小烦恼。我无法使用listpicker从“设置”页面更改主页上文本框中的字体。我已经创建了一个小测试应用程序,所以我不会搞砸我的真实应用程序。我希望有人可以提供帮助。

我所拥有的是2页应用主页和文本页面。我在主页上有一个文本框,我从文本页面的文本页面填充,我允许用户选择字体样式和颜色。我可以改变风格而不是颜色...一些文本框仅用于测试。 txtMain文本框是将包含其中字体的文本框。

炫魅

public partial class MainPage : PhoneApplicationPage
{
    private IsolatedStorageSettings appsettings = IsolatedStorageSettings.ApplicationSettings;

    public MainPage()
    {
        InitializeComponent();

        if (appsettings.Contains("font"))
        {
            txtMain.Text = appsettings["font"].ToString();
        }

        if (appsettings.Contains("fontColor"))
        {
            string newColor = appsettings["fontColor"].ToString();
            txtColor.Text = newColor;
        }

        if (appsettings.Contains("colorSelect"))
        {
            string _colorSelect = appsettings["colorSelect"].ToString();
            txtColorSelect.Text = _colorSelect;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/text.xaml", UriKind.Relative));
    }
}

文字页

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.IO.IsolatedStorage;

namespace test_font_color
{
    public partial class text : PhoneApplicationPage
    {
        private IsolatedStorageSettings appsettings = IsolatedStorageSettings.ApplicationSettings;
        public SolidColorBrush colorSelection;

        public text()
        {
            InitializeComponent();
            //**********************for list box*************************
            List<colorChoices> source = new List<colorChoices>();
            source.Add(new colorChoices() { pickedColorBlock = Colors.Black.ToString(), pickedColor = "Black", pickedSolidColorBrush = new SolidColorBrush(Colors.Black) });
            source.Add(new colorChoices() { pickedColorBlock = Colors.White.ToString(), pickedColor = "White", pickedSolidColorBrush = new SolidColorBrush(Colors.White) });
            source.Add(new colorChoices() { pickedColorBlock = Colors.Red.ToString(), pickedColor = "Red", pickedSolidColorBrush = new SolidColorBrush(Colors.Red) });
            this.lstColors.ItemsSource = source;
            this.lstColors.SelectionChanged += new SelectionChangedEventHandler(lstColors_SelectionChanged);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (appsettings.Contains("font"))
            {
                appsettings.Remove("font");
                appsettings.Add("font", txtEnter.Text);
            }
            else
            {
                appsettings.Add("font", txtEnter.Text);
            }

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));              
        }

        public class colorChoices
        {
            //public Color pickedColorBlock
            public string pickedColorBlock
            {
                get;
                set;
            }
            public string pickedColor
            {
                get;
                set;
            }
            public SolidColorBrush pickedSolidColorBrush
            {
                get;
                set;
            }
        }

        void lstColors_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            colorSelection = ((colorChoices)(lstColors.Items[lstColors.SelectedIndex])).pickedSolidColorBrush;
        }

        private void changeColor_Click(object sender, RoutedEventArgs e)
        {
            txtEnter.Foreground = colorSelection;
            appsettings.Add("fontColor", txtEnter.Foreground);
            appsettings.Add("colorSelect", colorSelection);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在App.xaml代码中定义自定义SolidColorBrush:

<SolidColorBrush x:Key="FontColor"
                         Color="Blue" />

使用StaticResource

将其设置为您的控件
Foreground = {StaticResource FontColor};

在代码中,它可以像这样访问:

((SolidColorBrush)App.Current.Resources["FontColor"]).Color

每个颜色更改都将自动更新为使用此资源的每个元素。