问题
I am writing a ConfigManager class using Portable Class Libraries. PCL supports StreamReader
and StreamWriter
classes that I want to use, but the PCL version of those classes do not support passing in a string during construction. PCL also does not support the reader.Close()
and writer.Close()
. Lastly it doesn't support the FileStream
class.
So I am looking for an answer to any one of the following questions:
- How can I get the
StreamReader
andStreamWriter
classes working in a PCL? - How can I create a new
stream
using PCL? - What other alternitives do I have to load and save files in a PCL?
回答1:
Use Dispose()
instead of Close()
(or just wrap everything in a using statement). We've hidden/removed Close()
in Windows Store apps and newer PCLs, because it does the same thing and people would be confused about which one to call.
Consider using PCL Storage for cross platform file system access.
Here are some blog posts you may want to refer to for how to approach platform-specific functionality in PCLs:
- How to Make Portable Class Libraries Work for You
- Portable Class Library Enlightenment / Adaptation
- Using Target-Specific Code in a Portable Library
回答2:
found the answer over here (by Rob Caplan): http://social.msdn.microsoft.com/Forums/windowsapps/en-US/386eb3b2-e98e-4bbc-985f-fc143db6ee36/read-local-file-in-portable-library#386eb3b2-e98e-4bbc-985f-fc143db6ee36
File access cannot be done portably between Windows Store apps and Windows Phone 8 apps. You will have to use platform specific code, to open the file and acquire a stream. You can then pass the stream into the PCL.
Since both Windows Store apps and Windows Phone 8 apps use the essentially the same Windows (Phone) Runtime classes from Windows.Storage to open files you can share the code (but not the binary) by linking a code file between the two projects. See Share code with Add as Link .
See Maximize code reuse between Windows Phone 8 and Windows 8 for more techniques for sharing code.
If anyone has a solution other than this I would be interested to hear it; also wondering about the .Close()
methods in the PCL.
来源:https://stackoverflow.com/questions/18110324/streamreader-and-portable-class-library