Sunday, January 17, 2010

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.

Tuesday, September 8, 2009

July 2008 - Posts - Guy Burstein's Blog

July 2008 - Posts - Guy Burstein's Blog: "How To: Call a Java EE Web Service from a .Net Client

Call a Java EE Web Service .Net InteroperabilityMany organizations have server side investments in Java technologies. While they want to build a compelling UI with Microsoft’s latest technologies, such as WPF and Silverlight, they still want to benefit from those existing investments instead of rewriting them. In order to do so, we have to bridge between those technologies and allow client side technologies consume Java web services.

This post is a step by step guide for building a Java EE Web Service, and a .Net client application that consumes it.

Before we get started with this walkthrough, make sure you have the following installed on your machine:

* Java Development Kit (JDK) 6
* Java EE 5 SDK
* NetBeans 6.1 IDE (Web & Java EE)

Create a Java Web Service (Java EE, JAX-WS)
1. Create a new Web Application

In the NetBeans 6.1 IDE, choose File –> New Project. In the New Project Dialog select the Web category, and choose Web Application from the projects list. Then, Click Next.

* If the web category is not available in this dialog, it means that the NetBeans version you have installed isn’t the Web and Java EE package.

Java EE Web Service .Net Client Interoperability

In the Name and Location page, set the location where you want to create the web application, and provide a name for the project. Click Next.

Java EE Web Service .Net Client Interoperability

In the Server and Settings page, leave the default settings (Java EE 5, Use GlassFish V2) and Click Finish.

Java EE Web Service .Net Client Interoperability

This creates the initial web application and opens the project for editing.

Java EE Web Service .Net Client Interoperability
2. Create the Web Service

Add a new web service to the project. Right click the project node and choose New –> Web Service.

Java EE Web Service .Net Client Interoperability

* Notice that the location of the Web Service option in the menu may change from this image and your IDE.

In the New Web Service dialog, provide the Web Service Name, and the Package. The name of the service will affect the final URL for calling the service, and the package name will be the namespace of the service contract. Click Finish.

Java EE Web Service .Net Client Interoperability

The Web Service now appears in the project tree.

Java EE Web Service .Net Client Interoperability

To implement the service, double click the service node in the project tree (in the figure above – CalculatorService). This will open the Web Service in Design mode, that lets you graphically design your service.

Java EE Web Service .Net Client Interoperability

Change to Source View by clicking on the Source button in the upper toolbar, and this will open the CalculatorService.Java file for editing. Here is a sample implementation of the service. Notice how Java Annotations are similar to .Net Attributes, especially how similar they are to the Web Services attributes we know…

package org.bursteg.calculator;



import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;



@WebService

public class CalculatorService

{

@WebMethod

public int Add(@WebParam(name='a') int a,

@WebParam(name='b') int b)

{

return a + b;

}

}

Deploy the web service to the web application server. From the NetBeans IDE this is done by right clicking the project node, and choosing Undeploy and Deploy.

Java EE Web Service .Net Client Interoperability

After the web application has been deployed, just to make sure the web service works as expected, you can right click the web service node, and choose Test Web Service.

Java EE Web Service .Net Client Interoperability

This will open the browser and navigate to a test page with the url of the service (http://localhost:9232/Calculator/CalculatorServiceService) with a ?Tester suffix.
Call the Java Web Service from a .Net Client

In Visual Studio 2008, create a new console application.

Java EE Web Service .Net Client Interoperability

This creates a new solution with a single Console Application project in it.

Right click the project node and choose Add Service Reference.

Java EE Web Service .Net Client Interoperability

In the Add Service Reference Dialog, paste the address of the service metadata endpoint (service address + ?wsdl suffix: http://localhost:9232/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service metadata and understand the service contract.

Java EE Web Service .Net Client Interoperability

Provide a namespace for the service reference, and click OK.

This will generate the client side proxy that lets you consume the service easily, and the required configuration settings into the configuration file.

Java EE Web Service .Net Client Interoperability

To call the service using the generated client side proxy, open Program.cs and use the following code:

static void Main(string[] args)

{

CalculatorServiceClient proxy = new CalculatorServiceClient();

int result = proxy.Add(2, 3);

Console.WriteLine('Calculator Service returned: ' + result.ToString());

}

Run the program and see that the web service is being called and the result is correct.
Conclusion

Since Java EE Web Services (JAX-WS) are standard SOAP services, they are easily interoperable from a .Net client application with only several clicks. Visual Studio generated a .Net client proxy that makes it very easy to connect and call the service.

Enjoy!"