【问题标题】:Getting relative path inside a custom component获取自定义组件内的相对路径
【发布时间】:2013-09-15 10:22:05
【问题描述】:

我有一个自定义组件项目,我指的是另一个项目。

我需要在自定义组件的页面之间导航,所以我使用了这段代码:

var frame = Application.Current.RootVisual as PhoneApplicationFrame;
frame.Navigate(new Uri("/CustomComponent;component/Page.xaml", UriKind.Relative));

现在,假设我的自定义组件具有这样的结构:

Root Folder
       \_ Page.xaml
       \_ Folder A
                 \_ BaseClass.cs
       \_ Folder B
                 \_ Folder C
                           \_ Class.cs (extends BaseClass)
                           \_ Page2.xaml

我想在Class.cs 中调用一个方法,该方法返回一个字符串,允许我使用我发布的导航代码导航到Page2.xaml

所以,这个方法应该返回

/CustomComponent;component/文件夹B/文件夹C/Page2.xaml

/CustomComponent;component/ 是可选的,但我需要它来返回正确的文件夹结构以导航到页面)

我一直在尝试使用 Directory 类,但 GetCurrentDirectory() 方法返回指向应用程序安装文件夹的绝对路径,我需要它是遵循组件结构的相对路径。

【问题讨论】:

    标签: c# xaml windows-phone-8 navigation uri


    【解决方案1】:

    这是一个获取调用函数的绝对路径的函数:

        public static string GetFilePathLoc([CallerFilePath] string filePath = "")
        {
            return filePath;
        }
    

    您可以发布流程以仅提取相关部分,例如通过创建 RootClass 并从中和 Class1 中调用 GetFilePathLoc:

        public abstract class BaseClass
    {
        public string GetResourceFilePath()
        {
            string assemblyName = Assembly.GetExecutingAssembly().FullName;
            assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(','));
    
    
            string myFilePath = GetRealClassPath();
    
            //If base class is in the root directory, this could just be replaced by string projectFolder = GetFilePathLoc();
            string projectFolder = RootClass.GetPath();
    
            //Remove the "absolute part" of the path
            myFilePath = myFilePath.Substring(projectFolder.Length);
             myFilePath=myFilePath.Replace('\\', '/');
    
            return "/" + assemblyName + ",component/" + myFilePath;
        }
    
        protected abstract string GetRealClassPath();
    
        protected static string GetFilePathLoc([CallerFilePath] string filePath = "")
        {
            int indexFileName= filePath.LastIndexOf('\\');
            return filePath.Substring(0, indexFileName+1);
        }
    }
    
    
    public class Class1 :BaseClass
    {
        protected override string GetRealClassPath()
        {
            return GetFilePathLoc(); 
        }
    }
    

    【讨论】:

    • 我已经编辑了这个问题,因为我忘了说我的班级扩展了另一个班级。因此,将此代码添加到BaseClass 将使其返回该类的路径,即使它是从派生类调用的。我是否必须在每个子类中复制并粘贴此代码才能使其正常工作?
    • 唯一需要在 class1 上的代码是 GetFilePathLoc() 调用。我修改了我的示例,将大部分代码放在 BaseClass 中
    • 我仍然无法正常工作。从具有此路径 plugins/widgets/PRIVATE/Class.cs 的类运行时,它返回 /Component;component/IVATE\Class.cs
    • 已修复(添加了一个部分以删除 GetFilePathLoc 中的文件名并在 GetResourceFilePath 中将 \ 替换为 /),我这次对其进行了测试,所以它应该可以工作:),只要确保你把 RootClass (或BaseClass 如果您在库的根目录中使用了字符串 projectFolder = GetFilePathLoc();)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多