简体   繁体   English

file.exists 返回 false xamarin android

[英]file.exists returns false xamarin android

i'm trying to use a font file in my Xamarin.android app.我正在尝试在我的 Xamarin.android 应用程序中使用字体文件。 I want to use it in itext7 library.我想在 itext7 库中使用它。 but I got exceptions on my setfont function saying that it's empty though I had my font files added to the set so I tried to check if my font file exists using file.exist function.但是我的 setfont 函数出现异常,说它是空的,尽管我将字体文件添加到了集合中,所以我尝试使用 file.exist 函数检查我的字体文件是否存在。 I tried this code:我试过这个代码:

a = File.Exists("C:/Windows/Fonts/Arial.ttf");
           
            if (a==true)
            {
                Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);

                alert.SetTitle("exist");

                alert.SetMessage("exists!");
                alert.SetPositiveButton("Okay", (senderAlert, args) =>
                {
                    alert.Dispose();
                });

                _dialog = alert.Create();

                _dialog.Show();
               

            }
            else
            {
                Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);

                alert.SetTitle("exist");

                alert.SetMessage("doesn't exist!");
                alert.SetPositiveButton("Okay", (senderAlert, args) =>
                {
                    alert.Dispose();
                });

                _dialog = alert.Create();

                _dialog.Show();
            }

the result was that the file doesn't exist.结果是文件不存在。 though the physically exits in the mentioned path and it is a font file found in the system it's not a file that I downloaded.尽管物理存在于上述路径中,并且它是在系统中找到的字体文件,但它不是我下载的文件。 I checked the permissions I found that the permission of the directory font is read-only for all the files in it.我检查了权限发现目录字体的权限对里面的所有文件都是只读的。 I tried using other fonts too.我也尝试使用其他字体。 I don't understand why is it returning false.我不明白为什么它返回 false。 thanks in advance提前致谢

Here is how to integrate fonts into xamarin Android: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/resources-in-android/fonts以下是如何将字体集成到 xamarin Android: https ://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/resources-in-android/fonts

  1. Package the font as an Android resource – this ensures that the font is always available to the application, but will increase the size of the APK.将字体打包为 Android 资源——这可确保字体始终可供应用程序使用,但会增加 APK 的大小。
  2. Download the fonts – Android also supports downloading a font from a font provider.下载字体 – Android 还支持从字体提供商下载字体。 The font provider checks if the font is already on the device.字体提供程序检查字体是否已经在设备上。 If necessary, the font will be downloaded and cached on the device.如有必要,字体将被下载并缓存在设备上。 This font can be shared between multiple applications此字体可以在多个应用程序之间共享

The first way is the simplest:第一种方式是最简单的:

Packaging a font into an Android APK ensures that it is always available to the application.将字体打包到 Android APK 中可确保它始终可供应用程序使用。 A font file (either a .TTF or a .OTF file) is added to a Xamarin.Android application just like any other resource, by copying files to a subdirectory in the Resources folder of a Xamarin.Android project.通过将文件复制到 Xamarin.Android 项目的 Resources 文件夹中的子目录,将字体文件(.TTF 或 .OTF 文件)添加到 Xamarin.Android 应用程序,就像任何其他资源一样。 Fonts resources are kept in a font sub-directory of the Resources folder of the project.字体资源保存在项目资源文件夹的字体子目录中。

And if you want to access it in the code:如果你想在代码中访问它:

Android.Graphics.Typeface typeface = this.Resources.GetFont(Resource.Font.caveat_regular);
textView1.Typeface = typeface;
textView1.Text = "Changed the font";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM