当前位置: 首页 > 知识库问答 >
问题:

未找到与给定名称Xamarin Android匹配的资源

程祺
2023-03-14

我试图在Xamarin实现一个文件提供程序。窗体,我有一个问题加载我的文件路径文件。每当我试图构建该应用程序,我得到以下错误:

没有找到与给定名称匹配的资源(在“资源”处,值为“@xml/file_paths”)

文件包含多个路径。xml文件位于xml文件夹下的resources目录中。任何帮助都将不胜感激。以下是我的相关档案:

Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" package="com.mycompany.myapp" android:versionName="1" android:installLocation="internalOnly">
<uses-sdk android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/advanced" android:label="FD CANNECT" android:windowSoftInputMode="adjustPan" android:descendantFocusability="afterDescendants" >
    <provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="com.myapp.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true">
        <meta-data 
          android:name="android.support.FILE_PROVIDER_PATHS" 
          android:resource="@xml/file_paths" />
    </provider>
</application>
</manifest>

文件路径。xml

<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="downloads" path="downloads/" />
</paths>

主要活动。反恐精英

using Android.Provider;
using Android.Support.V4.Content;
using CAN2LAN.Droid;
using Java.Lang;

[assembly: Xamarin.Forms.Dependency(typeof(ShareIntent))]

namespace CAN2LAN.Droid
{
using Acr.UserDialogs;
using System.Linq;
using Xamarin.Forms;

using Xamarin.Forms.Platform.Android;

global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        UserDialogs.Init(this);
        base.OnCreate(bundle);
        global::Xamarin.Forms.Forms.Init(this, bundle);

        Downloaded();

        Android.Runtime.AndroidEnvironment.UnhandledExceptionRaiser += (sender, args) => {
            args.Handled = false;
        };

        this.LoadApplication(new App());
    }

    public void Downloaded() {
        CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>(file => {
            string fileName = Android.Net.Uri.Parse(file.Url).Path.Split('/').Last();
            return System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads).AbsolutePath, fileName);
        });
    }


}

public class ShareIntent : IShareable {
    public void OpenShareIntent(string fileName) {         
        OpenFile(fileName);
    }

    public void OpenFile(string fileName) {
        string auth = "com.myapp.fileprovider";
        System.Diagnostics.Debug.WriteLine("Auth is " + auth);
        string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(fileName.ToLower()));
        if (mimeType == null)
            mimeType = "*/*";
        Android.Net.Uri uri = null;
        System.Diagnostics.Debug.WriteLine("Created authentication and mime type");
        var file = new Java.IO.File(System.IO.Path.Combine(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads).AbsolutePath, fileName));
        System.Diagnostics.Debug.WriteLine("File size is " + file.TotalSpace);
        System.Diagnostics.Debug.WriteLine("Froms context is " + Forms.Context);
        try {
             uri = FileProvider.GetUriForFile(Forms.Context, auth, file);
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Exception " + e);
        }

        System.Diagnostics.Debug.WriteLine("URI is "  + uri);


        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, mimeType);
        intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission);
        intent.AddFlags(ActivityFlags.NewTask | ActivityFlags.NoHistory);

        System.Diagnostics.Debug.WriteLine("Intent is created");
        // Trying to allow writing to the external app ...
        var resInfoList = Forms.Context.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
        foreach (var resolveInfo in resInfoList) {
            var packageName = resolveInfo.ActivityInfo.PackageName;
            Forms.Context.GrantUriPermission(packageName, uri, ActivityFlags.GrantWriteUriPermission | ActivityFlags.GrantPrefixUriPermission | ActivityFlags.GrantReadUriPermission);
        }
        System.Diagnostics.Debug.WriteLine("We have created res info lsit");
        try
        {
            System.Diagnostics.Debug.WriteLine("We are about to show the intent");
            Forms.Context.StartActivity(intent);
            System.Diagnostics.Debug.WriteLine("We ahe shwoed the intent");

        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("We have caugth an exception " + e);
        }


    }
}
}

共有3个答案

慕容宏邈
2023-03-14

确保XML文件都是小写的。那帮我修好了。

郎河
2023-03-14

我相信这个问题的真正答案是由@bradykey提到的

"将构建操作更改为Android资源"

可以通过右键单击文件并选择“属性”来执行此操作。在这里,您可以选择构建操作。

濮阳振
2023-03-14

我没有试过,但这可能是个问题。您的文件路径似乎有问题。xml文件。你在说出你的名字

name="downloads" path="downloads/"

这些可能不同

要么是这个

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path
        name="files"
        path="external_files"/>
</paths>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    <files-path name="files" path="."/>
</paths>

欲了解更多信息,您可以访问此

编辑1

检查文件路径。xml文件生成操作,它应该是AndroidResource

 类似资料: