문제

Hey, I am trying to create a BarCode input box. On it a textblock is showing a preview of what the text input will look like in the specified Barcode type. I have a bunch of .TTF files that are the barcode fonts, they have been used in a WinForms app so I am sure they are correct. I try to load them to memory (don't want to install them) using:

sBarCodeFonts = new PrivateFontCollection();
        unsafe
        {
            fixed (byte* p = Resources.Code39)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code39.Length);
            }

            fixed (byte* p = Resources.Code128b)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code128b.Length);
            }

            fixed (byte* p = Resources.i2of5)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.i2of5.Length);
            }

            fixed (byte* p = Resources.ean13)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.ean13.Length);
            }
        }

This seems to load the PrivateFontCollection correctly, quickwatch indicates so.

In the Barcode class, I have a MyFontFamily property, which contains the System.Media.FontFamily that corresponds to the loaded file. This property is loaded like this:

MyFontFamily = new System.Windows.Media.FontFamily(sBarCodeFonts.Families[0].Name);

And it seems to be loaded correctly as well.

Then, I have a Barcode object, and I'm using a TextBlock to display it's text, using it's FontFamily:

<TextBlock Text="{Binding Path=MyBarcode.TextContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
                   FontFamily="{Binding Path=MyBarcode.MyFontFamily, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}" 
                   Name="txt"
                   Grid.Row="2" />

The TextBlock displays the text using a default font every time. I've debugged and the FontFamily is correctly set to one of the loaded ones in the previous C# code.

Any clues?

Thanks.

EDIT: Trying to simplify the problem, I've created a very quick and dirty test app to load the TTF and show it, this is the only code (besides the XAML with only a grid):

System.Windows.Media.FontFamily lFamily = new System.Windows.Media.FontFamily(new Uri(@"E:\Prototypes\TestApp\Resources\Code128b.ttf", UriKind.Absolute), "Code128bWin");
            TextBlock lTextBlock = new TextBlock();
            lTextBlock.Text = "jsldkasjLKJOSDIFUEWR32849792837.,´` ";
            lTextBlock.FontFamily = lFamily;
            lTextBlock.FontSize = 50.0;
            grid.Children.Add(lTextBlock);

And it still shows the default font.

도움이 되었습니까?

해결책

<FontFamily x:Key="MyFont">/WpfTest;component/Resources/#Airplanes in the Night Sky</FontFamily>

<TextBlock FontFamily="{StaticResource MyFont}">Hello world</TextBlock>

Given that when you open the .ttf the "Font name: .." is "Airplanes in the Night Sky" and your project namespace is "WpfTest" and you dragged the font into a folder called "Resources"

다른 팁

I don't know about the method you're using, but I've successfully embedded fonts in a WPF application before by adding the files to the project as resources, and then creating a WPF/XAML resource in markup to reference the font file (using a pack Uri). This doesn't require the fonts to be installed on the machine - they're embedded in your .exe or a .dll file.

I did not find any answer for exactly this. But I found a solution that I did not see on Internet.

I followed a recommendation of doing a folder and marking all the files inside as Resources. But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.

I found this as a solution to list all the files inside my resources/fonts folder

Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))

I expect it to help you organize your fonts.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top