2013-01-01 29 views
2

我正在構建一個metro風格的應用程序。我設計了一個「設置」彈出窗口,用戶可以在其中更改應用程序的HomePageView頁面中包含的文本框的字體。如何從「設置」彈出框更新頁面的控件

通過列出所有系統字體的組合框選擇字體。一旦選擇了字體(在設置彈出框的組合框中),HomePageView頁面中的所有文本塊都必須更新。

這是樣式更新(位於standardstyles.xaml):

<Style x:Key="timeStyle" TargetType="TextBlock"> 
     <Setter Property="FontWeight" Value="Bold"/> 
     <Setter Property="FontSize" Value="333.333"/> 
     <Setter Property="FontFamily" Value="Segoe UI"/> 
    </Style> 

這是我使用更新的文本塊風格,在那裏我訪問SetTextBlockFont屬性更新的TextBlocks外觀代碼:

private void fontBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      var res = new ResourceDictionary() 
      { 
       Source = new Uri("ms-appx:///Common/StandardStyles.xaml", UriKind.Absolute) 

      }; 
      var style = res["timeStyle"] as Style; 

      style.Setters.RemoveAt(2); 
      style.Setters.Add(new Setter(FontFamilyProperty, new FontFamily("Arial"))); 

      HomePageView homePageViewReference = new HomePageView(); 
      homePageViewReference.SetTextBlockFont = style; 
     } 

這是HomePageView.xaml.cs更新一個文本塊(timeHour)的SetTextBlockFont屬性:

public Style SetTextBlockFont 
     { 
      set 
      { 
       timeHour.Style = value; 
      } 
     } 

該應用程序編譯沒有錯誤,但是當我點擊組合框中的字體時,沒有任何反應。我想因爲我必須加載HomePageView頁面的新實例homePageViewReference或因爲我不得不重新加載頁面或類似的東西。

我指出我不能使用Frame對象或NavigationService類,因爲這是一個metro應用程序。

回答

1

您需要在視圖中實現INotifyPropertyChanged,或者您可以直接使用LayoutAwarePage給出的DefaultViewModel。

Class A:INotifyPropertyChanged 
{ 

    #region EventHandler 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void RaisePropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 
    public Style SetTextBlockFont 
    { 
     set 
     { 
      timeHour.Style = value; 
      RaisePropertyChanged("SetTextBlockFont"); 
     } 
    } 
} 
+0

謝謝你的回答,但我仍然有問題。 正如我上面寫的,我選擇字體的文本框和我必須更改字體的文本塊位於不同的頁面中。 我無法通知不同頁面之間風格的變化。 希望有人能幫助我。 – user1941332

+0

你可以保留一個單例類,或者你可以使用app.xaml.cs並使用Property。所以,當你使用全局屬性導航到頁面時,你可以改變不同頁面的樣式。 – nucleons

+0

你能舉個例子嗎?我從未使用全局屬性和單例類。我知道,它們很少被使用。非常感謝您的幫助 – user1941332

相關問題