Test User Control

Source: Internet
Author: User

Window phone has something called user control. You can create custom controls. Try it out today. First. Right-click a project and choose add> create.
Name the control forecastinfo. Add code in layout of XAML:
<Grid. columndefinitions> <columndefinition width = "*"/> <columndefinition width = "*"/> </grid. columndefinitions> <grid. rowdefinitions> <rowdefinition Height = "*"/> <rowdefinition Height = "*"/> </grid. rowdefinitions> <textblock grid. column = "0" grid. row = "0" X: Name = "day" horizontalalignment = "center" verticalignment = "TOP" text = "" fontsize = "40"/> <textblock grid. column = "0" grid. row = "1" X: Name = "whether" Horizontalalignment = "center" verticalignment = "TOP" fontsize = "40" text = "7℃ ~ 9 ℃ "/> <image X: Name =" wimg "grid. column = "1" grid. row = "0" grid. rowspan = "2" verticalalignment = "center" horizontalalignment = "Left"/>

Display Effect:
To assign a value to the image on the control. (This is also true for assigning values to other controls). You need to add an attribute.

public String ImageUri { get; set; }

And assign a value to the source of the image in the load event. The background code of the entire forecastinfo:

public string ImageUri { get; set; }        private void UserControl_Loaded(object sender, RoutedEventArgs e)        {            BitmapImage bmp = new BitmapImage(new Uri(ImageUri, UriKind.Relative));            WImg.Source = bmp;        }
In this way. The user control is complete. Next. Add this control on mainpage. First, add a namespace on mainpage. XAML.
xmlns:my="clr-namespace:Wp7WhetherForecast"

The namespace rule is followed by the project name. My project is wp7whetherforecast. Then add the control:

<my:forecastInfo  x:Name="fore" HorizontalAlignment="Left" VerticalAlignment="Top"                            Width="300" Height="100" Margin="0,0,0,0"                                ImageUri="/Wp7WhetherForecast;component/Images/sunday.jpg"/>

Previously. I have added an image to the project.
The building attribute of the image is resource. Run, succeeded!
PS: When you do not run it at the beginning, you will be prompted that the <forecastinfo cannot be found when you add it, and the blue wavy line is displayed under forecastinfo. However, the smart prompt display can be found. You don't have to worry about this problem. Just save and run it. If you want to assign values to other controls on the user control, you can perform the same operations as assigning values to images.

Related Article

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.