Windows 8 Custom Controls Example

Using the Windows 8 Developer Preview, I am trying to use a simple user control created using the built-in Windows Metro style template. So far, I have not been able to get my application to allow a link to a control, even if it is in the same project and namespace as the page that links to it. I just get "Type was not found" I looked at the "Build" samples and could not find a sample C # project that uses a user control. Does anyone know where I can find him?

+4
source share
3 answers

Tick http://asyncui.codeplex.com/SourceControl/changeset/view/7969#139603

You can create a UserControl by right-clicking on your project and selecting Add / New Item (Ctrl + Shift + A) and selecting User Control from the list of item templates. Then you name it and you end up with XAML, which you can change to add more user interface, for example here:

 <UserControl x:Class="Xyzzer.AsyncUI.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="1366"> <Grid x:Name="LayoutRoot" Background="#FF0C0C0C"> <Grid VerticalAlignment="Top" Height="140"> <Grid.ColumnDefinitions> <ColumnDefinition Width="120" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button x:Name="BackButton" IsEnabled="False" /> <TextBlock x:Name="PageTitle" Text="Some Page!" Grid.Column="1" /> </Grid> </Grid> </UserControl> 

and code like this:

 using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Windows.Foundation; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Data; namespace Xyzzer.AsyncUI { partial class MainPage { public MainPage() { InitializeComponent(); } } } 

Then you can use this control elsewhere in XAML as follows:

 <xa:MainPage xmlns:xa="using:Xyzzer.AsyncUI" /> 
+2
source

Even if the user control is in the same namespace, you will need to declare a namespace in order to use it in XAML. Try adding an attribute to the page element.

 xmlns:uc="YourApplication.Namepace" 

and the prefix of your XAML control

 <uc:YourUserControl />. 

Make sure your project is built when you are not referencing a user control. An error in the user control will cause exactly this problem.

+1
source

If you use the default project template for the metro application, the page namespace is usually called local, it is already on the new page created from the template, as shown below.

 xmlns:local="using:App1" 

Note that "using:" is the new syntax in a metro application.

That way you can reference your user control as

 <local:MyUserControl1/> 

If you drag the MyUserControl toolbar from the xaml constructor, all this is automatic. You do not need to write code for this.

0
source

Source: https://habr.com/ru/post/1395939/


All Articles