我希望你能帮助我解决我的问题。我有一个简单的地铁应用程序。名为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的设置窗格(条形图 - >设置 - &gt;设置),其中放置了名为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变量中,以在关闭并重新打开应用程序时保留其值。
解决方案有效,但问题是:当我进入设置窗格(魅力栏 - &gt;设置 - &gt;设置)并通过滑块更改textblock fontsize属性时,我回到主页视图并重新打开设置如果窗格,fontsize属性重新初始化为其原始值,因此它不保留以前(用户选择的)值。 你能修好我的代码吗?非常感谢你提前。
答案 0 :(得分: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();
}