gpt4 book ai didi

Silverlight 随机字体错误

转载 作者:行者123 更新时间:2023-12-04 06:37:16 26 4
gpt4 key购买 nike

我在 Silverlight 项目中添加了一些自定义字体。然而,他们的行为非常零星。我在自定义控件中使用它。当我第一次添加控件时,它显示正常。当我重建时,字体变回默认值。当我在浏览器中查看应用程序时,它也使用默认字体。字体作为资源嵌入。

Silverlight issue

顶部是我将控件添加到设计器后的右侧,底部是浏览器中的应用程序。我不知道是什么原因造成的。如果您需要控件的代码,我可以提供。

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); }
}
}
}

最佳答案

好的,我明白问题出在哪里,但我不知道如何在程序上解决这个问题。

问题是指定 FontFamily如果您不使用它的名称/路径对其进行限定,例如 Fonts\mynewfont.ttf#My New Font,则自定义字体的名称无关紧要(即它不起作用)。 .上面的代码所做的只是说“提出 My New Font”,在运行时它不理解,因为找不到它。

解决此问题的一种方法是将您需要的字体作为 App.xaml 中的资源并以这种方式使用它们,例如:<FontFamily x:Key="YanoneKaffeesatzThin">Fonts\Yanone Kaffeesatz-47.ttf#Yanone Kaffeesatz Thin</FontFamily> 1 然后从代码中调用该家族的应用程序:myTxtBox.FontFamily = DirectCast(App.Current.Resources("YanoneKaffeesatzThin"), FontFamily) .

1Yanone Kaffeesatz Thin 来自 Google 字体库

关于Silverlight 随机字体错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com