Windows 8 用户控件示例

发布于 2025-01-04 19:51:18 字数 181 浏览 1 评论 0原文

使用 Windows 8 开发人员预览版,我尝试使用我使用内置 Windows Metro 样式模板创建的简单用户控件。 到目前为止,我还无法让我的应用程序解析对控件的引用,即使它与引用它的页面位于同一项目和命名空间中。我只是得到“找不到类型” 我查看了“Build”示例,但找不到使用用户控件的示例 C# 项目。 有谁知道我在哪里可以找到一个?

Using the Windows 8 developer preview I'm trying to use a simple User Control I have created using the built in Windows Metro style template.
As yet I have not been able to get my application to resolve the reference to the control even though it is in the same project and namespace as the page referencing it. I just get "The type was not found"
I've looked through the "Build" samples and could not find an sample C# project that uses a user control.
Does anyone know where I could find one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2025-01-11 19:51:18

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

您可以通过右键单击您的项目并选择“添加/新项目”(Ctrl+Shift+A) 并从列表中选择“用户控件”来创建一个UserControl项目模板。然后,您可以对其进行命名,并最终得到 XAML,您可以修改该 XAML 以添加更多 UI,如下所示:

<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>

以及后面的代码,如下所示:

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();
        }
    }
}

然后,您可以在 XAML 中的其他位置使用该控件,如下所示:

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

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

You can create a UserControl by right clicking your project and selecting Add/New Item (Ctrl+Shift+A) and selecting "User Control" from the list of item templates. You then name it and end up with the XAML that you can modify to add more UI like 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 behind 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();
        }
    }
}

You can then use that control elsewhere in XAML like this:

<xa:MainPage
    xmlns:xa="using:Xyzzer.AsyncUI" />
弃爱 2025-01-11 19:51:18

即使用户控件位于同一命名空间中,您也必须声明该命名空间才能在 XAML 中使用它。尝试将属性添加到页面元素

xmlns:uc="YourApplication.Namepace"

并将前缀添加到 XAML 控件

<uc:YourUserControl />.  

确保在不引用用户控件时构建项目。用户控件中的错误就会导致此问题。

Even if the User Control is in the same namespace, you will have to declare the namespace to use it in the XAML. Try adding the attribute to your page element

xmlns:uc="YourApplication.Namepace"

and the prefix to your XAML control

<uc:YourUserControl />.  

Make sure that your project builds when you aren't referencing the User Control. An error in the user control will cause just this problem.

海风掠过北极光 2025-01-11 19:51:18

如果您使用 Metro 应用程序的默认项目模板,页面的命名空间通常命名为 local,它已经位于从模板创建的新页面中,如下所示。

xmlns:local="using:App1"

请注意,“using:”是 Metro 应用程序中的新语法。

因此,您可以引用您的用户控件,

<local:MyUserControl1/>

如果您将 MyUserControl 从工具箱拖到 xaml 设计器,所有这些都是自动的。您不需要为此编写任何代码。

If you are using the default project template of the metro app, the namespace of the page is typically named as local, it's already in the new page created from template as following.

xmlns:local="using:App1"

Note the "using:" is new syntax in metro app.

So you can reference your user control as

<local:MyUserControl1/>

If you drag the MyUserControl from toolbox to the xaml designer, all these are automatic. You don't need to write any code for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文