【问题标题】:Partial class with single part / No defining declaration found for implementing declaration of partial method具有单个部分的部分类/未找到用于实现部分方法声明的定义声明
【发布时间】:2019-02-24 11:12:24
【问题描述】:

我正在尝试在 Forms 应用程序的共享项目中实现一个简单的部分类和方法,以使用 Android 和 iOs 项目。

所有类都具有相同的命名空间 (sharedTest) 并具有关键字 partial,但 Android 和 iOs 文件中的部分类和方法似乎无法识别共享代码中实现的另一部分。

部分类共享说“部分类单部分”

Android和iOs文件中的PlatformGetFilePath方法抛出错误“No defined declaration found for implementation of partial method”

Android和iOs文件中的变量_filepath抛出“当前方法中不存在”

为了完成这项工作,我有什么遗漏吗?

共享项目中的部分类:

using System;
using Xamarin.Forms;

namespace sharedTest
{
    public partial class Shared
    {
        public Shared()
        {

        }

        partial void PlatformGetFilePath(string filename);
        string _filepath;
        public string GetFilePath(string filename)
        {
            PlatformGetFilePath(filename);
            return _filepath;
        }

    }
}

安卓:

using System;
using System.IO;
using Xamarin.Forms;

namespace sharedTest
{
    public partial class Shared
    {
        partial void PlatformGetFilePath(string filename)
        {

           string libraryPath=Environment.GetFolderPath(Environment.SpecialFolder.Personal); ;

            _filepath = Path.Combine(libraryPath, filename);
        }
    }
}

iOS:

using System;
using System.IO;
using Xamarin.Forms;

namespace sharedTest
{
    public partial class Shared 
    {
        partial void PlatformGetFilePath(string filename)
        {

            string documentsPath=Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            string libraryPath = Path.Combine(documentsPath, "..", "Library");

            _filepath = Path.Combine(libraryPath, filename);
        }
    }
}

【问题讨论】:

    标签: c# visual-studio xamarin cross-platform partial-classes


    【解决方案1】:

    首先,它不起作用,因为你可以在iOS和Android中使用相同的命名空间。如果你想在iOS和Android中做不同的事情,你可以使用Custom RenderersDependencyService。例如

    在表单中

    ...
    
    namespace sharedTest
    {
    
     public class MyClass
      {
        public interface IGetFilePath
        {
            String PlatformGetFilePath(string filename);
        }
      }
    
    }
    

    在 iOS 中

    using System;
    using System.IO;
    using sharedTest;//your xamarin.forms name
    using sharedTest.iOS;
    using Xamarin.Forms;
    
    [assembly: Dependency(typeof(MyiOSClass))]
    
    namespace sharedTest.iOS
    {
        public class MyiOSClass:MyClass.IGetFilePath
        {
            public MyiOSClass()
            {
    
            }
            public string PlatformGetFilePath(string filename)
            {
    
             string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
             string libraryPath = Path.Combine(documentsPath, "..", "Library");
    
             string filepath = Path.Combine(libraryPath, filename);
    
             return filepath;
            }
        }
    }
    

    在安卓中

    using System;
    using Xamarin.Forms;
    using sharedTest;//your xamarin.forms name
    using sharedTest.Droid;
    using System.IO;
    
    [assembly: Dependency(typeof(MyAndroidClass)) ]
    
    namespace sharedTest.Droid 
    {
        public class MyAndroidClass: MyClass.IGetFilePath
        {
            public MyAndroidClass()
            {
    
            }
    
            public string PlatformGetFilePath(string filename)
           {
    
             string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); ;
    
             string filepath = Path.Combine(libraryPath, filename);
    
             return filepath; 
           }
       }
    }
    

    在共享代码中实现

    var filePath = DependencyService.Get<MyClass.IGetFilePath>().PlatformGetFilePath("your file name"); 
    

    更多信息可以参考here

    【讨论】:

      【解决方案2】:

      这可以做到 - 但部分类的共享部分需要在“共享项目”项目类型中定义。

      我错误地认为这个“共享项目”可能是我的 Xamarin Forms .net 标准类库。它不能——“共享项目”一词指的是一种称为“共享项目”的实际项目类型——而不仅仅是共享的项目。

      您需要添加一个“共享项目”类型的新项目,然后从您的 Xamarin Forms 类库和您的平台特定项目中引用它。并确保命名空间与您所做的匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        相关资源
        最近更新 更多