2013-04-06 40 views
0

我希望你能幫助我解決我的問題。我有一個簡單的地鐵應用程序。名爲myTextBlock的文本塊位於HomePageView頁面中。這是它的XAML:如何將滑塊值鏈接到文本塊的FontSize並將該值存儲在ApplicationData變量中

<TextBlock x:Name="myTextBlock" Text="Hello" HorizontalAlignment="Center" VerticalAlignment="Top" Style="{StaticResource timeStyle}" TextAlignment="Center" FontSize="300" RenderTransformOrigin="0.5,0.5" Margin="11,55,3,0" ManipulationMode="All"> 

這是在主頁的代碼在LoadState背後:

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) 
     { 
      try 
      { 
       double? myTextBlockValue = (double?)ApplicationData.Current.LocalSettings.Values["myTextBlockSize"]; 
       if (myTextBlockValue != null) 
       { 
        myTextBlock.FontSize = (double)myTextBlockValue; 
       } 
      } 
      catch 
      { 

      } 
     } 

我建了一個設置面板(魅力吧 - >設置 - >設置)命名SettingsPage其中滑塊名爲timeSlider被放置。這是它的XAML:

<Slider x:Name="timeSlider" Width="257" Minimum="1" Maximum="600" Loaded="timeSlider_Loaded" ValueChanged="timeSlider_ValueChanged" /> 

而這些滑塊加載和的ValueChanged方法放在SettingsPage代碼隱藏:

private void timeSlider_Loaded(object sender, RoutedEventArgs e) 
     { 
      var sliderIstance = sender as Slider; 
      double? storedSize = (double?)ApplicationData.Current.LocalSettings.Values["myTextBlockSize"]; 
      if (storedSize != null && storedSize != 1) 
       sliderIstance.Value = (double)storedSize; 
      else 
       sliderIstance.Value = 300; 
     } 

     private void timeSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) 
     { 
      var sliderIstance = sender as Slider; 
      ApplicationData.Current.LocalSettings.Values["myTextBlockSize"] = sliderIstance.Value as double?; 
      var _Frame = Window.Current.Content as Frame; 
      _Frame.Navigate(_Frame.Content.GetType()); 
      _Frame.GoBack(); 
     } 

您可以在這裏下載完整的解決方案在我的SkyDrive:https://skydrive.live.com/redir?resid=1B721133DC03E67C!7350

通過這種方式,我將myTextBlock的fontsize屬性(在HomePageView頁面中)鏈接到滑塊的值屬性(在SettingsPage頁面中),以便用戶可以在運行時更改此屬性。此外,此屬性存儲在myTextBlockSize ApplicationData變量中,以在應用程序關閉並重新打開時保留其值。

該解決方案的工作原理是:當我進入設置窗格(魅力酒吧 - >設置 - >設置),並通過滑塊更改textblock fontsize屬性,然後我回到主頁視圖並重新打開設置窗格,fontsize屬性會重新初始化爲其原始值,因此它不會保留先前(用戶選擇的)值。 你能解決我的代碼嗎?非常感謝你提前。

回答

0

你必須在timeSlider_ValueChanged事件

private void timeSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) 
{ 
    var sliderIstance = sender as Slider; 
    if (sliderIstance.Value != 1) 
     ApplicationData.Current.LocalSettings.Values["myTextBlockSize"] = sliderIstance.Value as double?; 
    var _Frame = Window.Current.Content as Frame; 
    _Frame.Navigate(_Frame.Content.GetType()); 
    _Frame.GoBack(); 
} 
+0

謝謝你加一個條件if (sliderIstance.Value != 1)。你有解決方案。非常簡單! :) – user1941332 2013-04-09 20:36:40

相關問題