简体   繁体   中英

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.

Here is the code for my usercontrol (This is a dummy control but the problem exists with this):

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

What am I missing from my user control?

I'm using 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..

public MainPage()
    {
        this.InitializeComponent();

        hellousercontrol.Height = 100;
}

one more this..have build your solution ?

I had the same issue in c++ environment. 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. However without default constructor I was able to use it from within c++ code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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