locked
Loading local files from WinRT PCL

    Question

  • Hello.

    I have a WinRT PCL project which linked with Windows apps project. In PCL I have DirectX engine based on SharpDX and I want to load my content directly from PCL using relative pathes, but I cannot. ContentManager just doesnt see my folder.

    I tried with Package.Current.InstalledLocation.Path, but it return folder of main app (which using this PCL) and I didnt find a way to set correct local path to the project. It just dont see any path.

    When I did it from the Windows app, it works just fine, but from PCL it doesnt work at all.

    How can I make it see local path that I could make: Content.RootDirectory = "Content"; and evetything works correctly?

    P.S. No, I didnt forget to set build action - content in the properties.

    If someone know how to solve this issue, please, help me with it!


    Thursday, December 11, 2014 10:19 PM

Answers

  • Hi Black_Joker,

    Looks like I made small mistake here, GetManifestResourceStream Method (String) is acceptable in Windows Store App.

    I wrote some demo for you that can read a image from PCL and display on the main project, hope this helps:

            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                _assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
                Stream imageStream = _assembly.GetManifestResourceStream("ClassLibrary1.0.jpg");
                
    
                MemoryStream ms = new MemoryStream();
                imageStream.CopyTo(ms);
    
                IRandomAccessStream Mystream = await ConvertToRandomAccessStream(ms);
    
                var bitmapImage = new BitmapImage();
    
                await bitmapImage.SetSourceAsync(Mystream);
                img.Source = bitmapImage;
    
            }
    
            public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
            {
                var randomAccessStream = new InMemoryRandomAccessStream();
                var outputStream = randomAccessStream.GetOutputStreamAt(0);
                var dw = new DataWriter(outputStream);
                var task = Task.Factory.StartNew(() => dw.WriteBytes(memoryStream.ToArray()));
                await task;
                await dw.StoreAsync();
                await outputStream.FlushAsync();
                return randomAccessStream;
            }

    My Class structure:

    Final result:

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, December 15, 2014 10:11 AM
    Moderator
  • Put the assembly code in the class library can also works fine.

            public static Stream getImage()
            {
                Assembly _assembly;
                _assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
                Stream imageStream = _assembly.GetManifestResourceStream("ClassLibrary1.0.jpg");
                return imageStream;
            }

    Or you can do something with the Image Path, for instance "ms-appx:///ClassLibrary1/image.ipg"

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, December 16, 2014 9:46 AM
    Moderator

All replies

  • Hi Black_Joker,

    Where is your content located? In your PCL project or in your main project?

    I tried with Package.Current.InstalledLocation.Path, but it return folder of main app (which using this PCL) and I didnt find a way to set correct local path to the project. It just dont see any path.

    -> It's reasonable, you are using PCL, PCL is only offer code that can be used by main project, PCL does not have responsibility to storage files, you should put the file in the main project. But the question is why do you want to load the content from PCL?

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 12, 2014 9:53 AM
    Moderator
  • I want to write my own game editor, so I want editor to be windows project and engine itself - PCL to share engine between editor and game.

    My loading resources code is stored inside Game class, which is inside PCL.

    If I must contain resources outside the lib, how could I load them correctly? Could you provide some smal example for that case?

    Friday, December 12, 2014 10:10 AM
  • Hi Black_Joker,

    Looks like I made small mistake here, GetManifestResourceStream Method (String) is acceptable in Windows Store App.

    I wrote some demo for you that can read a image from PCL and display on the main project, hope this helps:

            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                _assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
                Stream imageStream = _assembly.GetManifestResourceStream("ClassLibrary1.0.jpg");
                
    
                MemoryStream ms = new MemoryStream();
                imageStream.CopyTo(ms);
    
                IRandomAccessStream Mystream = await ConvertToRandomAccessStream(ms);
    
                var bitmapImage = new BitmapImage();
    
                await bitmapImage.SetSourceAsync(Mystream);
                img.Source = bitmapImage;
    
            }
    
            public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
            {
                var randomAccessStream = new InMemoryRandomAccessStream();
                var outputStream = randomAccessStream.GetOutputStreamAt(0);
                var dw = new DataWriter(outputStream);
                var task = Task.Factory.StartNew(() => dw.WriteBytes(memoryStream.ToArray()));
                await task;
                await dw.StoreAsync();
                await outputStream.FlushAsync();
                return randomAccessStream;
            }

    My Class structure:

    Final result:

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, December 15, 2014 10:11 AM
    Moderator
  • Thanks for that.

    Say, if my loading resources logic is situated inside PCL, could I load resources just from PCL and not from outside?

    Tuesday, December 16, 2014 9:38 AM
  • Put the assembly code in the class library can also works fine.

            public static Stream getImage()
            {
                Assembly _assembly;
                _assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
                Stream imageStream = _assembly.GetManifestResourceStream("ClassLibrary1.0.jpg");
                return imageStream;
            }

    Or you can do something with the Image Path, for instance "ms-appx:///ClassLibrary1/image.ipg"

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, December 16, 2014 9:46 AM
    Moderator