2012-06-18 16 views

回答

4

假設你的用戶控件是在類似的格式:它應該在默認情況下,當你創建一個新的用戶控件和代碼背後

<UserControl x:Class="UserControlExample.NameReporter" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > 

<!-- Controls here --> 

</UserControl> 

類似於

using System.Text; 
using System.Windows; 
using System.Windows.Controls; 

namespace UserControlExample 
{ 
    public partial class NameReporter : UserControl 
    { 
     public NameReporter() 
     { 
      InitializeComponent(); 
     } 

     // your custom methods here 
    } 
} 

你應該再能夠使用類似於

的代碼將其添加到頁面
<Grid xmlns:src="clr-namespace:UserControlExample" 
     Background="White" Margin="0,50,0,0"> 
    <Grid.RowDefinitions> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <src:NameReporter Grid.Row="0"/> 
    <src:NameReporter Grid.Row="1" Margin="0,15,0,0"/> 
</Grid> 

Just改變clr-namespace:後的命名空間和控制名稱後src:

你可以把xmlns:標籤進入<phone:PhoneApplicationPage>標籤,而不是使用整個形式(而不是隻在一個網格)的控制,你可以改變src,無論你希望將其稱爲。

如果UserControl創建正確,編譯該解決方案應該意味着它出現在您的工具箱中以供使用,因此您只需拖放&即可。

請參閱參考資料以獲取更完整的示例。

參考文獻:

http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol%28v=VS.95%29.aspx

+0

謝謝。它完美的作品 –

3

對於Windows Phone 8的和Windows Phone 8.1,我能夠做完成這一任務的以下內容:

創建用戶控制。在這個例子中,我創建了幾個矩形來模仿經典的手機菜單按鈕。

Your usercontrol

生成解決方案。該項目將更新,您現在將在工具箱中看到您的用戶控件。

Toolbox

只需將您的用戶控制了工具箱,並在那裏你想使用你的用戶控件拖放到XAML頁面。

UserControl in use

相關問題