c# - Accessing the web from a MVVM Light ViewModel in design mode on WP8 -
i using mvvm light framework mvvm viewmodellocator , have method follows:
protected override system.threading.tasks.task<string> getfromuri(string uristring) { string outstring; taskcompletionsource<string> tcs = new taskcompletionsource<string>(); httpwebrequest request = (httpwebrequest)httpwebrequest.create(new uri(uristring, urikind.relativeorabsolute)); request.method = "get"; request.begingetresponse((result) => { webresponse response = request.endgetresponse(result); using (var instream = response.getresponsestream()) { long length = instream.length; byte[] inbytes = new byte[length]; instream.read(inbytes, 0, (int)length); outstring = encoding.utf8.getstring(inbytes, 0, (int)length); tcs.trysetresult(outstring); } } , null); return tcs.task; }
...which trying use in "design mode". seems best place call within a
if (isindesignmodestatic) { //.... }
in viewmodel's constructor. however, fails "cross thread" exception. wrapping call in dispatcher.invoke application.current.rootvisual gets past "cross thread" exception, results in "cannot access disposed object" instead (referencing dispatcher object).
using mvvm light framework on windows phone 8, can perform web request in design mode (i can't see technical reason why not)? if viewmodel constructor wrong place this, right place?
the issue originated using httpwebrequest. rewriting use httpclient (system.net.http library available on nuget) solved problem.
Comments
Post a Comment