【问题标题】:How to change the colour of font within a WP8 application如何在 WP8 应用程序中更改字体颜色
【发布时间】:2014-03-20 06:47:38
【问题描述】:

我正在为视障人士开发一个 WP8 应用程序,并且我正在尝试能够更改应用程序中字体的颜色。我不,没有一个 API 可以帮助我。我想做的是在 longlistselector 中有一个颜色列表,用户可以选择一种颜色,整个应用程序字体颜色会发生变化。我不是世界上最好的程序员,因为我刚刚开始,这个应用程序是针对我的一位家庭成员的。我坚持的部分是试图改变它,我可以选择它,但在那之后它无处可去,任何指针或提示都会很棒。

public MainPage()
{
    InitializeComponent();
    font.Add(new Theme1() { ThemeText = "White", ThemeFontSize = "40" });
    font.Add(new Theme1() { ThemeText = "Green", ThemeFontSize = "40" });
    font.Add(new Theme1() { ThemeText = "Blue", ThemeFontSize = "40" });

    LLsFontList.ItemsSource = font;
}


private void LLsFontList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (LLsFontList != null && LLsFontList.SelectedItem != null)
    {
       var selectedItem = LLsFontList.SelectedItem as Theme1;
       SayWords(selectedItem.ThemeText + "\r\n");
       var id = selectedItem.ThemeText.FirstOrDefault();
    }
}

这是我卡住的地方,我是否应该将此调用发送到资源文件以更改整个应用程序。

【问题讨论】:

  • 但是如何将主题应用于文本?什么是 Theme1 类?
  • 主题 1 类是所有 set 和 get 所在的地方

标签: c# windows-phone-8 fonts colors


【解决方案1】:

我的解决方案不是最优雅的,但我希望这能让你继续前进。

好的,你的 Theme 类应该是这样的:

public class Theme : INotifyPropertyChanged      
{
public event PropertyChangedEventHandler PropertyChanged;

private string themeText;
public string ThemeText
{
    get
    {
        return themeText;
    }
    set
    {
        themeText = value;
        OnPropertyChanged("ThemeText");
    }
}

private int fontSize;
public int FontSize
{
    get
    {
        return fontSize;
    }
    set
    {
        fontSize = value;
        OnPropertyChanged("FontSize");
    }
}

private Brush fontColor;
public Brush FontColor
{
    get
    {
        return fontColor;
    }
    set
    {
        fontColor = value;
        OnPropertyChanged("FontColor");
    }
}

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = this.PropertyChanged;
    if (handler != null)
        handler(this, new PropertyChangedEventArgs(name));
}   
}

然后在您的文本块和 Theme 类的对象之间创建绑定:

 <TextBlock x:Name="TextBlock" Text="{Binding ThemeText}" FontSize="{Binding FontSize}" Foreground="{Binding FontColor}"/>

关于代码隐藏:你应该有一个带有一些默认值的全局 Theme 对象:

Theme theme = new Theme
{
    ThemeText = "Red",
    FontColor = new SolidColorBrush(Colors.Red),
    FontSize = 40
};

然后将 TextBlock 的 DataContext 设置为它(在页面构造函数中):

TextBlock.DataContext = theme;

如果你想改变它,就这样做吧:

theme.ThemeText = "Blue";
theme.FontColor = new SolidColorBrush(Colors.Blue);
theme.FontSize = 60;

如果您有任何问题,请随时提出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多