Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Xamarin.Android中检索.ttf文件的路径 我想做什么_C#_Android_Fonts_Xamarin.android_Sdl - Fatal编程技术网

C# 在Xamarin.Android中检索.ttf文件的路径 我想做什么

C# 在Xamarin.Android中检索.ttf文件的路径 我想做什么,c#,android,fonts,xamarin.android,sdl,C#,Android,Fonts,Xamarin.android,Sdl,我需要访问consola.ttf文件,该文件已作为文件包含在我的资产文件夹中 我所做的 方法1: 尝试使用以下代码以流形式访问文件: AssetManager assets = MainActivity.Context.Assets; using (var stream = assets.Open("Fonts/consola.ttf")) { font = new SdlFont(ToBytes(stream), 15)

我需要访问consola.ttf文件,该文件已作为文件包含在我的资产文件夹

我所做的 方法1:

尝试使用以下代码以流形式访问文件:

        AssetManager assets = MainActivity.Context.Assets;
        using (var stream = assets.Open("Fonts/consola.ttf"))
        {
            font = new SdlFont(ToBytes(stream), 15);
        }
上面使用的ToBytes方法

static byte[] ToBytes(Stream stream)
        {
            byte[] b;
            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                ms.Flush();
                ms.Position=0;
                b = ms.ToArray();
            }
            return b;
        }
我发现这是不可能的,因为我使用的“”库显然不直接喜欢字节流

我的SDLFont包装器方法

public SdlFont(byte[] data, int fontSize)
        {
            Handle = OpenFontRW(OS.RWFromMemory(data, data.Length), 1, fontSize);
        }
使用SDLfont方法通过我的包装器调用TTF的此函数:

[DllImport(libTTF, EntryPoint = "TTF_OpenFontRW", CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr OpenFontRW(IntPtr src, int freesrc, int ptsize);
该TTF方法已记录在案。使用此选项只会引发未处理的异常并使应用程序崩溃

方法2:

正在尝试在运行时获取路径:

            var fontname = "consola.ttf";
            var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts);
            var fontpath = System.IO.Path.Combine(path,fontname);
            font = new SdlFont( fontpath , 15);
以下是它们在运行时的值:

这也不行

另一种解决方案是从系统本身访问我未包含的任何字体文件(.ttf)。不过,能够使用我自己的字体会更好

如果有人知道我如何在Android中访问系统字体的路径,那对我也会很有用

多谢各位