使用设置页面更改主页面的字体颜色
本文关键字:主页 字体 颜色 设置 | 更新日期: 2023-09-27 18:25:10
我正试图完成一个应用程序,但我还有一个小烦恼。我无法使用列表选择器从"设置"页面更改主页上文本框的字体。我创建了一个小的测试应用程序,这样我就不会把我的真实应用程序搞砸了。我希望有人能帮忙。
我有两页的应用程序主页和文本页。我在主页上有一个文本框,它是我从文本页面填充的,我允许用户选择字体样式和颜色。我可以改变风格,只是不能改变颜色。。。有些文本框只是为了测试。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" />
使用StaticResource 将其设置为您的控制
Foreground = {StaticResource FontColor};
在代码中,它可以像这样访问:
((SolidColorBrush)App.Current.Resources["FontColor"]).Color
每次颜色更改都会自动更新到使用此资源的每个元素。