0

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

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

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;组件/文件夹 B/文件夹 C/Page2.xaml

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

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

4

1 回答 1

0

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

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

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

    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(); 
    }
}
于 2013-09-15T19:51:04.017 回答