我正在编写一个Windows 8应用商店应用程序,并在其中设计了我自己的用户控件。
以下是我的用户控件的代码(这是一个虚拟控件,但问题存在于此):
<UserControl
x:Class="Windows8StoreTest.TestUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="70"
Height="40">
<StackPanel>
<Button Content="Hello" Foreground="Pink" BorderBrush="Pink"/>
</StackPanel>
</UserControl>我已经将用户控件放到我的页面上,并给它起了一个名字:
<Page
x:Class="Windows8StoreTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows8StoreTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:TestUserControl Name="testControl"/>
</Grid>
</Page>但是,当我转到后面的代码时,我不能使用该名称访问控件。它似乎并不存在!奇怪的是,这个控件不存在于MainPage类的InitializeComponent()方法中,这就是它存在的原因。
我在用户控件中遗漏了什么?
我使用的是Windows8应用商店、XAML、c#。
提前感谢
发布于 2013-07-12 15:30:10
你好,我不知道出了什么问题,但它应该可以工作。我刚刚做了一个示例。我把它放在这里,希望你也是这样做的。
<Page
x:Class="App12.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App12"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:MyUserControl1 x:Name="hellousercontrol" />
</Grid>
在我的mainpage.cs中..我只是像这样使用它..
public MainPage()
{
this.InitializeComponent();
hellousercontrol.Height = 100;
}再来一个this..have构建您的解决方案?
https://stackoverflow.com/questions/17596943
复制相似问题