简体   繁体   English

无法通过代码访问Windows 8 Store用户控件

[英]Can't access Windows 8 Store user control from code

I'm writing a Windows 8 Store application and within that I've designed my own user control. 我正在编写Windows 8 Store应用程序,并在其中设计了自己的用户控件。

Here is the code for my usercontrol (This is a dummy control but the problem exists with this): 这是我的usercontrol的代码(这是一个虚拟控件,但存在此问题):

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

I've dropped the user control onto my page and give it a name: 我已将用户控件拖放到页面上并为其命名:

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

However, when I go to the code behind I can't access the control by that name. 但是,当我转到后面的代码时,无法使用该名称访问控件。 It doesn't seem to exist! 它似乎不存在! What is weird is that the control doesn't exists within InitializeComponent() method for the MainPage class which will be why it does exist. 奇怪的是,该控件在MainPage类的InitializeComponent()方法中不存在,这就是它确实存在的原因。

What am I missing from my user control? 我的用户控件缺少什么?

I'm using Windows 8 Store, XAML, c#. 我正在使用Windows 8 Store,XAML,c#。

Thanks in advance 提前致谢

Try to use this: 尝试使用此:

<local:TestUserControl x:Name="testControl"/>

Should work... 应该管用...

hello i don't know what is wrong but it should work.i have just made a sample example of it..i am putting it here hope you have done the same way. 您好,我不知道出什么问题了,但是应该可以。我刚刚举了一个示例示例。.我把它放在这里,希望您也可以这样做。

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

in my mainpage.cs.. i have just use it like this.. 在我的mainpage.cs中。我只是这样使用它。

public MainPage()
    {
        this.InitializeComponent();

        hellousercontrol.Height = 100;
}

one more this..have build your solution ? 一个..已经建立您的解决方案?

I had the same issue in c++ environment. 我在c ++环境中遇到了同样的问题。 I observed, I didn't had default constructor in my class, as soon as I added the default constructor, I could use the defined UserControl in my project through XAML file. 我观察到,我的类中没有默认构造函数,一旦添加了默认构造函数,便可以通过XAML文件在项目中使用定义的UserControl。 However without default constructor I was able to use it from within c++ code. 但是,没有默认构造函数,我可以在c ++代码中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM