【问题标题】:How to update controls of a page from a "settings" flyout如何从“设置”浮出控件更新页面控件
【发布时间】:2013-01-02 21:23:17
【问题描述】:

我正在构建一个地铁风格的应用程序。我设计了一个“设置”弹出窗口,用户可以在其中更改应用的 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 属性以更新文本块外观的代码:

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 类,因为这是一个地铁应用程序。

【问题讨论】:

    标签: c# windows-8 microsoft-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");
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答,但我仍有问题。正如我上面所写的,我选择字体的组合框和我必须更改字体的文本块位于不同的页面中。我无法通知不同页面之间样式的变化。希望有人可以帮助我。
    • 您可以保留一个单例类,或者您可以使用 app.xaml.cs 并使用属性。因此,当您使用全局属性导航到页面时,您可以更改不同页面中的样式。
    • 你能给我举个例子吗?我从未使用过全局属性和单例类。我知道,它们很少使用。非常感谢您的帮助
    猜你喜欢
    • 2023-03-31
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多