簡體   English   中英

如何在WP8應用程序中更改字體的顏色

[英]How to change the colour of font within a WP8 application

我正在為視覺障礙者開發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();
    }
}

這是我遇到的問題,應該將此調用發送到資源文件,以便它更改整個應用程序。

我的解決方案不是最優雅的,但是我希望這可以幫助您。

好的,這是您的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;

如有任何疑問,請隨時詢問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM