Parallel initial experience of iPhone + WP7

Source: Internet
Author: User
Tags button attributes silverlight
Learn iPhone and WP7 development at the same time (1)

Last year, when I saw that the iPhone was very popular, I went to buy an iPhone development starter classic. At that time, I didn't have so many books (now there are bookshelves of iPhone development books in the bookstore ), this entry-level Classic is recommended by many people on the Internet. I rushed to buy it and read the first chapter. Sorry, I often work overtime at in the middle of the night. I don't want to touch my computer when I come back to sleep. The books are full of dust. Recently, with time, I thought that I could not make 69 yuan of white money. So I wiped the dust on the book skin and started learning about iPhone development. I have no experience in C/C ++ or obj-C, so I can click C #. I am still not proficient in it, and I have never had a Mac. It starts from scratch. Then I had another whimsy. When I was learning about iPhone development, I also learned how to develop WP7. Although Silverlight, xNa, WPF, and so on won't happen at all, there is also the language base of C, at least no syntax is required.

Prepare the environment:

WP7:Download vm_web2.exe and run it quickly if the network is smooth.

These are the installed tools:

  IPhone: Without a Mac, you can only install it on a virtual machine. For how to install it, just follow the instructions in the tutorial on the Internet. VMware is installed with snow leopard 10.6.0 and can be upgraded to 10.6.8 through software updates. 3.2.4 is used for xcode and 4.1 is used for iOS SDK. Why is it not the latest SDK.

  

Three Hello World:

After the environment is ready, let's write three Hello World, One WP7, and two iPhones. Why are two of them? One is written by monotouch.

  WP7: Open vs2010 express for WP, new --- Project

Vm_web2 downloads the wp7.1 SDK, so you can see that there is VB in the Project template. In the past, 7.0 was not compiled using VB. Of course, we still use C # for writing.

Select window phone application. After confirmation, wp7.0 or wp7.1 is displayed. After selection, vs creates the project directory.

We start from scratch, no SL, no xNa, and then drag two widgets, buttons, and text labels from Toolbox into winform.

Set the tag and button attributes, double-click the button, generate an event, and write code in the event .. Familiar operation steps. Same as winform.

Generated XAML code:

View
Code

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">   <TextBlock Height="96" HorizontalAlignment="Left" Margin="73,81,0,0" Name="lbShow" Text="" VerticalAlignment="Top" Width="311" FontSize="56" TextAlignment="Center" />   <Button Content="Click" Height="72" HorizontalAlignment="Left" Margin="144,231,0,0" Name="btnClick" VerticalAlignment="Top" Width="160" Click="btnClick_Click" FontSize="28" /> </Grid>
Show sourceview
Sourceprint?

C # code:

View
Code

public partial class MainPage : PhoneApplicationPage    {        // Constructor        public MainPage()        {            InitializeComponent();        }        private void btnClick_Click(object sender, RoutedEventArgs e)        {            lbShow.Text = "Hello World";        }    }

Analysis:You can think about ASP. NET. Use HTML for pages and C # For logic writing. Use XAML to describe the UI, or use C # To write the logic.

Summary:From the first simple helloworld demo, I have never touched Silverlight, but winform or webform has no obstacles. One line of code, two delays, implementation is super simple.

IPhone:Open the VM, start Mac OS, open xcode, file ---- new project ---- select IOS ---- application --- view-based application.

Xcode has created the project directory structure like vs2010. Referring to the entry-level classic I bought a year ago, and the first class of the iPhone Development Open Course at Stanford University, I tried to wait for two hours before making a hello that works just as well as WP7.
World. Here is a summary of the twists and turns.

Double-click hello_worldviewcontroller.xib in the resources directory. Interface builder is called to open the XIB file.

Similarly, drag a text label, round rect button, and the button in the iPhone is a rounded corner button. After dragging it to the interface, it is not that easy. Follow the train of thought, now, you should write an event for the button. When you click the button, the text tag is changed to hello World, split it, and you only need to understand two things. 1. How do I set the tag value? xxx. Text = "XXX? 2. How to add an event to the button. Double-click cannot be done now.

First, you can use the code to read or set the property value of the control, that is, to operate the control. Unlike in WP7, you can drag out the control. You must first define one in the code, and then associate it with the link dragged out in interface builder, and set its get and set. To operate the button, you must first define it.

In xcode, select hello_worldviewcontroller.h and define:

View
Code

#import <UIKit/UIKit.h>@interface Hello_WorldViewController : UIViewController {    IBOutlet UILabel *lbShow;}@property (retain nonatomic) UILabel *lbShow;-(IBAction)changeLabelText:(id)sender; @end

After the definition is completed, associate it in IB. Press ctrl to drag the file's owner to the label Association attribute, right-click the button, select the touch up inside Event, click the plus sign next to it, and drag it to the file's Owner Association event.

Now, we have finished the same work as dragging the two controls out of WP7. You can write the logic.

Implement the changelabeltext method just defined in hello_worldviewcontroller.m. Look at the parameter sender next to it and think about the same thing as the object sender parameter of the event in WP7. The logic code is just a line of code, just like WP7. But there is a lot to do for this line of code first ..

View
Code

@synthesize lbShow;- (void)changeLabelText:(id)sender {    lbShow.text = @"Hello World";}

Click build and run. The difference between opening a program in the simulator and WP7 is that WP7 is called emulator and IP is called simulator.

Analysis:Aside from the syntax differences between C # And obj-C, xcode's ability to automatically generate code from the UI is not powerful, and its attributes and events must be defined and re-associated. In this way, coupling is reduced, just like the difference between webform and MVC.

Summary:The logic is the same, data ("Hello World"), logic ("changelabeltext"), and display (XAML/XIB ). Organize the data, draw the interface, and use different logic to control the display. Similarly, to write a hello World, WP7 is much faster than the iPhone. VS is able to write a row twice, while xcode + interfacebuilder is able to write eight lines ..

Monotouch version:

  OBJ-C is a tough nut to crack. A Hello world has been around for a long time, so I also tried to use monotouch to check whether WP7 is so "efficient". The mono version is 2.10.5, and monodevelop is 2.6.

About monotouch, there is an article in the knowledge base in the garden. From installation to use, Lao Zhao also wrote how to use vs2010 to engage in monotouch. Choose iPhone and iPad -- iphone window based application to create a new solution. Note that monodevelop does not support entering Chinese characters. Chinese characters are garbled and the language is changed to English in settings. After the creation is successful, for example, the kind C #

Double-click the XIB file and call interface builder to open the file. It repeats the same work as xcode development. Drag the control. You do not need to define it in the code, but use appdelegate to add the association. Save the settings in IB and you will find that the added controls and corresponding events are automatically generated in XXX. XIB. Designer. CS with the same name.

View
Code

[MonoTouch.Foundation.Register("AppDelegate")]    public partial class AppDelegate {                private MonoTouch.UIKit.UIWindow __mt_window;                private MonoTouch.UIKit.UILabel __mt_lbShow;                #pragma warning disable 0169        [MonoTouch.Foundation.Export("changeLabelText:")]        partial void changeLabelText (MonoTouch.UIKit.UIButton sender);        [MonoTouch.Foundation.Connect("window")]        private MonoTouch.UIKit.UIWindow window {            get {                this.__mt_window = ((MonoTouch.UIKit.UIWindow)(this.GetNativeField("window")));                return this.__mt_window;            }            set {                this.__mt_window = value;                this.SetNativeField("window", value);            }        }                [MonoTouch.Foundation.Connect("lbShow")]        private MonoTouch.UIKit.UILabel lbShow {            get {                this.__mt_lbShow = ((MonoTouch.UIKit.UILabel)(this.GetNativeField("lbShow")));                return this.__mt_lbShow;            }            set {                this.__mt_lbShow = value;                this.SetNativeField("lbShow", value);            }        }    }

Well, the distribution method only needs to be implemented.

View
Code

// The name AppDelegate is referenced in the MainWindow.xib file.    public partial class AppDelegate : UIApplicationDelegate    {        // This method is invoked when the application has loaded its UI and its ready to run        public override bool FinishedLaunching (UIApplication app, NSDictionary options)        {            // If you have defined a view, add it here:            // window.AddSubview (navigationController.View);            window.MakeKeyAndVisible ();                return true;        }                partial void changeLabelText (MonoTouch.UIKit.UIButton sender)        {            lbShow.Text = "Hello World";        }            // This method is required in iPhoneOS 3.0        public override void OnActivated (UIApplication application)        {        }    }

After saving, click the gear to run and the simulator pops up smoothly.

Here are two icons. The first one is developed by xcode, followed by monotouch.

Analysis:The code generated by monotouch is to translate obj-C into C #. It seems that the IOS library is packaged into C # library and then called using C, monotouch is a proxy responsible for converting C # Into ios sdk calls.

Summary:C # programmers can use it easily without learning obj-C. tools are also very handy. In terms of development efficiency, writing is slower than WP7 and faster than xcode (with less UI-defined code, IB is automatically generated after being dragged)

The last step is to set the xcode 99-knife-free real machine debugging program tutorial. I am unable to deploy the real machine because xcode4 + ios4.3, only available 3.2.4 + ios4.1 (My 3gs is 4.1 and has never been upgraded)

 

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.