2013-07-11 60 views
1

中存儲用戶控件我在寫一個Windows 8商店應用程序,並在其中設計了我自己的用戶控件。無法訪問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()方法內,這就是它存在的原因。

我從用戶控件中遺漏了什麼?

我正在使用Windows 8 Store,XAML,c#。

在此先感謝

+0

它應該工作。你能分享一些更多的信息來縮小這個問題嗎? –

回答

1

嘗試使用此:

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

應該工作...

0

你好,我不知道什麼是錯誤的,但它應該幹活剛纔提出的it..i我把它在這裏希望你也做了同樣的方式採樣的例子。

<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構建解決方案?

+0

該控件似乎沒有被添加到InitializeComponent方法和該類中!我會更新我的問題以包含完整的代碼 – Sun

0

我曾在相同C++的問題的環境。我觀察到,在我的類中沒有默認構造函數,只要我添加了默認構造函數,就可以通過XAML文件在我的項目中使用已定義的UserControl。但是,如果沒有默認構造函數,我可以在C++代碼中使用它。