Domanda

I am working on a JavaScript metro application, and I found out that to call a C# code from JavaScript I have to use Windows Runtime Component project in my solution. Now I need to check some image which are added in the JavaScript metro application in "images" folder and if they are there I need to copy them in the application local folder.

First I tried using the method

Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///images/logo.png"))

but it throws exception that could not find the location/ file path.

I thought it might be because the namespaces of project are different. The image is added in JavaScript metro application project and I am trying to use it from a Windows Runtime Component project.

So I tried passing a hard coded path for the image file like "E://projects/myProject/myProject/images/logo.png", and tried with method

Windows.Storage.StorageFile.GetFileFromPathAsync(<passed the above path>); 

but it throws exception that file path is not correct format.

Can I access a file added in JavaScript metro application project from Windows Runtime Component project in the same solution? And copy it in the application local folder?

thanks.

È stato utile?

Soluzione

This should work, and I tried your first line of code in a new JS project with a new WinRT component in C#. It worked just fine. Can you show more of the whole C# class in your component as well as the JS code that's calling it?

Here's the .cs code for my test:

using System;
using Windows.Storage;

namespace Test2
{
    public sealed class Class1
    {
        public async void TestAccess()
        {
            StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///images/logo.png"));
        }
    }
}

And the JS code to call it:

var obj = new Test2.Class1();
obj.testAccess();            

And have you also confirmed that the logo.png file is in your JS project?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top