2

Silverlight プロジェクトにいくつかのカスタム フォントを追加しました。ただし、彼らの行動は非常に散発的です。カスタムコントロールで使用しています。最初にコントロールを追加すると、正常に表示されます。再構築すると、フォントがデフォルトに戻ります。ブラウザーでアプリを表示すると、既定のフォントも使用されます。フォントはリソースとして埋め込まれます。

Silverlight の問題

上はデザイナーにコントロールを追加した直後、下はブラウザー内のアプリです。何が原因なのかわかりません。コントロールのコードが必要な場合は、提供できます。

BorderedTextBlock.xaml

<UserControl x:Class="MindWorX.CustomPropertyReproduction.Controls.BorderedTextBlock"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Height="100" Width="200">

    <Grid x:Name="LayoutRoot">
        <Border BorderThickness="1" BorderBrush="Lime">
            <TextBlock Name="MainTextBlock" Margin="4" TextWrapping="Wrap" />
        </Border>
    </Grid>
</UserControl>

BorderedTextBlock.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MindWorX.CustomPropertyReproduction.Controls
{
    public partial class BorderedTextBlock : UserControl
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(BorderedTextBlock), new PropertyMetadata("TextBlock", new PropertyChangedCallback(OnTextChanged)));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.Text = (String)e.NewValue;
        }

        new public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(BorderedTextBlock), new PropertyMetadata(new FontFamily("Portable User Interface"), new PropertyChangedCallback(OnFontFamilyChanged)));

        private static void OnFontFamilyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontFamily = (FontFamily)e.NewValue;
        }

        new public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(Double), typeof(BorderedTextBlock), new PropertyMetadata(11d, new PropertyChangedCallback(OnFontSizeChanged)));

        private static void OnFontSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BorderedTextBlock sender = (d as BorderedTextBlock);
            sender.MainTextBlock.FontSize = (Double)e.NewValue;
        }

        public BorderedTextBlock()
        {
            InitializeComponent();
        }

        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        new public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }

        new public Double FontSize
        {
            get { return (Double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }
    }
}
4

1 に答える 1

3

さて、ここで問題が何であるかはわかりますが、これを手順で修正する方法がわかりません。

問題はFontFamily、カスタムフォントをのような名前/パスで修飾しない場合、カスタムフォントの指定は重要ではない(つまり、機能しない)ことですFonts\mynewfont.ttf#My New Font。上記のコードが実行しているのMy New Fontは、実行時に見つからないために理解できない「起動」と言っているだけです。

これを回避する1つの方法は、App.xamlでリソースとして必要なフォントを作成し、次のように使用することです。1<FontFamily x:Key="YanoneKaffeesatzThin">Fonts\Yanone Kaffeesatz-47.ttf#Yanone Kaffeesatz Thin</FontFamily>次に、コードからこのファミリのアプリケーションを呼び出しますmyTxtBox.FontFamily = DirectCast(App.Current.Resources("YanoneKaffeesatzThin"), FontFamily)

1 YanoneKaffeesatzThinはGoogleFontsライブラリからのものです

于 2011-01-20T18:53:40.810 に答える