Part 8 of the Silverlight Tutorial: Use WPF to create a Digg desktop application

Source: Internet
Author: User

[Original address] Silverlight tutorial Part 8: creating a Digg desktop application using WPF
[Original article] Friday, February 22,200 AM

This is the eighth part of the eight series of tutorials. This series demonstrates how to use beta1 of Silverlight 2 to build a simple Digg client application. These tutorials are intended to read in order and help explain some of the core programming concepts of Silverlight.

Use WPF to create a Digg desktop application

The purpose of the last tutorial is a little different from that of the first seven. We will not actually run it in Silverlight in this tutorial.CodeInstead, WPF and. Net 3.5 will be used. We will use the existing Digg application code written in the browser to run it in the form of a Windows desktop application.

With Silverlight, a set of API subsets that are compatible with the. NET Framework full version are released. The purpose is to allow developers to learn a common programming model and tool set, and to reuse the learned skills between Ria web applications, rich Windows desktop applications, and office solutions, code and content.

Below are the steps I have taken to reuse our existing Digg Silverlight application code (running in a browser) to build a version of a Windows desktop application (running outside the browser ).

Step 1: Create a New WPF desktop application

We will first start to use vs 2008 to create a new WPF desktop application. We name it "digg‑topsample ":

This will create a project in Vs, containing two files, one app. XAML and one window. XAML:

Note that the structure of this project is similar to that of the Silverlight application created in the first article of this series of tutorials (with an app. XAML file and a page. XAML file ).

Step 2: copy the code of the current Digg application to the WPF application.

We will copy/paste the existing diggapplication Silverlight code into our new digg‑topsample windows project:

Currently, in beta1, this copy/paste operation is a manual step, and we finally have an automated way to move code between different project types.

Step 3: Correct Several Problems

I need to make two changes to compile the current Digg routine code successfully:

1) The namespace xmlns defined by Silverlight beta1 XAML: the URL is different from the complete WPF desktop version. I need to change the XAML file I copied into the new project to point to the complete WPF definition. This is something we are still working on before the release.

2) I need to change the <& lt; watermarktextbox> Control to <textbox> and the NoYou do not need to change any Code related to these controls, such as network calling, LINQ to XML, or data binding code.

After completing these minor changes, the project can be compiled cleanly.

Step 4: host the Digg application and in the desktop window

Then I open the windows1.xaml file in the desktop project (it is the default window for loading at app startup ).

I changed the Form title to "Digg desktop version", which expanded the default width and height of the window.

Then I add the page. XAML user control from the previous Digg Silverlight project to the window as the root control. The control is loaded and visible during window loading. I don't need to change the page class code or change the name of the page class. Because it is inherited from usercontrol, it can be fully hosted in any WPF window or control.

The last thing I changed is that the Digg rest API server will try to check whether it is accessed by a non-Browser/Server, sometimes a problem is caused by a denial of access response (probably preventing automatic scripts from accessing their services) in these cases. I solved this problem by letting the network access go through a proxy URL (no code changes, but URL changes ).

Step 5: run the application

Then, I can run our new Digg desktop application. All functions work the same as the Silverlight version, and the application behavior is the same:

After selecting a story from the list, the detail user control is displayed:

There are several minor style differences between the browser version and the desktop version. This is mainly because WPF inherits the default style (font, color, scroll bar, and so on) based on the OS theme selected by the current user by default ), silverlight has a default topic that we use on all operating systems. If we want to make the desktop version and browser version absolutely consistent, we can specify it more specifically in our style and control template. Otherwise, the desktop version will be slightly changed based on the user's OS topic.

Conclusion

We will introduce more detailed precautions and recommended best practices for sharing code between Silverlight and WPF projects in the future. I think you will find that the skills and knowledge you have learned when building a Silverlight application can be well transferred to a complete WPF project. We are also committed to a very high level of compatibility, promoting good code reuse between solutions, facilitating easy sharing and utilization of controls, content and code.

I hope this article will help you,

Scott

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.