0

我希望你能帮助我解决我的问题。我有一个简单的地铁应用程序。一个名为 myTextBlock 的文本块被放置在 HomePageView 页面中。这是它的 xaml:

<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">

这是后面 HomePageView 代码中的 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" />

这些是放置在 SettingsPage 代码隐藏中的滑块 Loaded 和 ValueChanged 方法:

 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 页面中)链接到滑块的 value 属性(在 SettingsPage 页面中),因此用户可以在运行时更改此属性。此外,此属性存储在 myTextBlockSize ApplicationData 变量中,以在应用程序关闭和重新打开时保留其值。

解决方案有效,但问题是:当我进入设置窗格(charm bar -> settings -> settings)并通过滑块更改 textblock fontsize 属性时,然后我回到主页视图并重新打开设置窗格,fontsize 属性被重新初始化为其原始值,因此它不会保留以前的(用户选择的)值。你能修复我的代码吗?非常感谢您提前。

4

1 回答 1

0

if (sliderIstance.Value != 1)您必须在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();
}
于 2013-04-08T09:36:13.477 回答