2

メトロスタイルアプリを作成しています。ユーザーがアプリの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;
        }

これは、テキストブロック(timeHour)を更新するHomePageView.xaml.csのSetTextBlockFontプロパティです。

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

アプリはエラーなしでコンパイルされますが、コンボボックス内のフォントをクリックしても何も起こりません。HomePageViewページhomePageViewReferenceの新しいインスタンスをロードする必要があるため、またはページなどをリロードする必要があるためだと思います。

これはメトロアプリであるため、FrameオブジェクトまたはNavigationServiceクラスを使用できないことを指摘します。

4

1 に答える 1

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");
        }
    }
}
于 2013-01-02T13:23:17.907 に答える