C# 使用“设置”页面更改主页上的字体颜色

C# 使用“设置”页面更改主页上的字体颜色,c#,windows-phone-8,fonts,C#,Windows Phone 8,Fonts,我正试图完成一个应用程序,我还有一个小烦恼。我无法使用listpicker从设置页面更改主页上文本框的字体。我已经创建了一个小的测试应用程序,这样我就不会弄乱我真正的应用程序。我希望有人能帮忙 我有2页的应用程序主页和文本页。我在主页上有一个文本框,它是我从文本页面填充的,我允许用户选择字体样式和颜色。我可以改变样式而不是颜色…一些文本框只是为了测试。txtMain文本框是将包含字体的文本框 主页 public partial class MainPage : PhoneApplicationP

我正试图完成一个应用程序,我还有一个小烦恼。我无法使用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);
        }
    }
}

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

<SolidColorBrush x:Key="FontColor"
                         Color="Blue" />
在代码中,它可以如下访问:

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

每次颜色更改都会自动更新到使用此资源的每个元素。

我不明白你的意思。您需要一个设置页面,用户可以在其中选择颜色和字体。在主页面中,您希望txtMain中的文本具有选定的颜色和字体,例如黑色和Arial?对。在“设置”页面上,用户将输入所需文本,选择字体,然后选择字体的颜色。然后,这将覆盖在他们先前选择的照片上,该照片将显示在主页上
((SolidColorBrush)App.Current.Resources["FontColor"]).Color