Dr. Vivek Murthy
5 years ago
James Wang's Blog
void OnMouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();
mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
}
//Application scope style which can be used through the application
//Apply style
-
-
-
-
-
In this simple example, I set allow other site to visit api subpath and its subpaths, such as "http://www.udatasoft.com/api/order.html"., without this file in the root of udatasoft.com, we can not establish a http call from a silverlight program.
2.Sync http call.
In silverlight , we can only make a async http call as follow:
void MakeAsyncCall(string url)
{
WebClient wb = new WebClient();
wb.DownloadStringCompleted +=new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
wb.DownloadStringAsync(new Uri(url));
}
void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
DisplayResult(e.Result);
else
txtHtml.Text = e.Error.Message;
}
In order to make a sync call, we need use XMLHttpRequest as follow:
public class SyncCall
{
protected static System.Windows.Browser.ScriptObject XMLHttpRequest;
protected static System.String Result = System.String.Empty;
///
/// Makes XMLHttpRequest
///
/// relative or absolute url as string
/// GET or POST
public static void MakeSyncCall(string Url, string Method)
{
// Backward compat
System.Windows.Browser.HtmlPage.Window.Eval("if(typeof XMLHttpRequest==\"undefined\"){XMLHttpRequest=function(){var a=[\"Microsoft.XMLHTTP\",\"MSXML2.XMLHTTP\",\"MSXML2.XMLHTTP.3.0\",\"Msxml3.XMLHTTP\"];for(var b=0;b// create the instance
XMLHttpRequest = System.Windows.Browser.HtmlPage.Window.CreateInstance("XMLHttpRequest");
// request data from server
XMLHttpRequest.Invoke("open", Method.ToUpper(), Url, false);
// send. !!! do not replace it with "null" as it throws exception
XMLHttpRequest.Invoke("send", "");
// get result as string. as gecko based browsers does not return xml
Result = XMLHttpRequest.GetProperty("responseText").ToString();
}
///
/// Returns result from request as string
///
public static System.String GetResult()
{
return Result.ToString();
}
}
3. Sync call WCF webservice in silverlight:
In silverlight, there are no way to make a sync call, because silverlight only provide way to make a async call to a web service. In some circumtance, we may need may sync call to webservice, or we at least limit user operation before a web service call finishes. One way to do that is to block the UI thread before web service methods call finished, but it will make web service call never call back, because microsoft process web service call in UI thread, if you block UI thread, your web service async call never be run. To deal with it, we can create a second thread to make a async call and set a flag in the UI thread by using dispather class when web service finished. Here we should notice that we can not directly change UI thread variables in the second thread because of thread conflicts, but we can using dispather class to run a short program in UI thread as follow:
private void btnWCFSyn_Click(object sender, RoutedEventArgs e)
{
txtHtml.Text = "";
new Thread(new ThreadStart(delegate
{
ServiceReference1.Service1Client wclientsyn = new SilverlightSync.ServiceReference1.Service1Client();
wclientsyn.GetBooksCompleted += (sender1, args) => { mnREvent.Set(); DisplayHtml(args); };
mnREvent.Reset();
wclientsyn.GetBooksAsync();
mnREvent.WaitOne(); })).Start();
//mnREvent.WaitOne();
}
void DisplayHtml(System.ComponentModel.AsyncCompletedEventArgs e)
{
SilverlightSync.ServiceReference1.GetBooksCompletedEventArgs e1=(SilverlightSync.ServiceReference1.GetBooksCompletedEventArgs)e;
Dispatcher.BeginInvoke(delegate()
{
txtHtml.Text = "";
if (e1.Error == null)
{
for (int i = 0; i < e1.Result.Count; i++)
txtHtml.Text += e1.Result[i] + "\n";
}
else
txtHtml.Text = "error: " + e1.Error.Message;
}
);
}
}