Saturday, November 7, 2009

一步一步学Silverlight 2系列(7):全屏模式支持 - TerryLee's Tech Space - 博客园

一步一步学Silverlight 2系列(7):全屏模式支持 - TerryLee's Tech Space - 博客园: "private void toggleButton_Click(object sender, RoutedEventArgs e) { Content contentObject = Application.Current.Host.Content; contentObject.IsFullScreen = !contentObject.IsFullScreen; }"

Silverlight Full Screen


private void toggleButton_Click(object sender, RoutedEventArgs e)
{
Content contentObject = Application.Current.Host.Content;
contentObject.IsFullScreen = !contentObject.IsFullScreen;
}

//Captch full screen event
public Page()
{
InitializeComponent();
Application.Current.Host.Content.FullScreenChanged += new EventHandler(Content_FullScreenChanged);
}

Silverlight mouse envent process




private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
Point p = e.GetPosition(e.Source as FrameworkElement); //get the position
Status.Text = String.Format("坐标位置({0}:{1})",p.X,p.Y);
}
private void ParentCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
String msg = "x:y = " + e.GetPosition(sender as FrameworkElement).ToString();
msg += " from " + (e.Source as FrameworkElement).Name;
Status.Text = msg;
}
void OnMouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
trackingMouseMove = false;
element.ReleaseMouseCapture();

mousePosition.X = mousePosition.Y = 0;
element.Cursor = null;
}


Silverlight style



//Application scope style which can be used through the application





//Apply style



Add property to customized control


为用户控件添加属性

简单的修改一下上面示例中的XAML文件,添加一个文本块控件,用它来显示文字提示信息。


Opacity="0.7" Fill="#FF8A8A8A"/>









HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="50 20 0 0"/>


Tuesday, November 3, 2009

My Works-Nov 3

9:00am-18:30pm
Have lunch with Xiaowei,jun and other teammates.

Compelte a solution for making sync call in silverlight.
1.Cross domain visit in silverlight:

test cross domain visit by creating a clientaccesspolicy.xml file and upload to my own web hosting udatasoft.com. In order to make a visit to another domain in silverlight by using WebClient, we need to upload a clientaccesspolicy.xml file to root directory of the remote domain. The following example is my test:



-
-
-
-


-







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;

}
);
}
}

Monday, November 2, 2009

My Works-Nov 2

8:30am-18:50, lunch break 12:30-13:20
In the morning, waiting for my computer to be setup.
Afternoon: install dev env and did a research how to do syn call in silverlight.