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.