C#如何使组件,其次是在 XAML 中重用?

标签: AngularJS WPF xaml C#
发布时间: 2017/2/24 2:47:47
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我决定使该控件,然后重用。作为中角的指令。 但只有达到广告。

namespace Chainhub.Forms.UI.Controls
{
    public partial class BoxPickerControl : ContentView
    {
        public BoxPickerControl()
        {
           InitializeComponent();
        }
    }
}

在 xaml 示例中的 BoxPickerControl

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Chainhub.Forms.UI.Controls.BoxPickerControl">
  <StackLayout>
    <StackLayout>
    <StackLayout  BackgroundColor="#383940" Padding="5,5,5,5"  Orientation="Horizontal">
      <StackLayout  HorizontalOptions="StartAndExpand">
        <Label Text="Categories"   TextColor="White"></Label>
      </StackLayout>
</ContentView>

注册并在内容页中调用

<controls:BoxPickerControl>
</controls:BoxPickerControl>

并成功地抓到


目标调用异常


我有什么做错了?

解决方法 1:

若要创建可重用的控件,您应该创建 UserControl ,然后添加一些必要的控件里面你 UserControl 。例如,我们正在创建 UserControl ,它会被称为 FooUserControl :

<UserControl x:Class="OpenExcelFileAndConvertToArray.FooUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:OpenExcelFileAndConvertToArray"
         mc:Ignorable="d">
   <Grid>
       <StackPanel Orientation="Horizontal">
           <TextBlock Text="SomeText"/>
           <Button Content="Delete"/>
       </StackPanel>            
   </Grid>
</UserControl>

然后只是在其他任何控件,您可以重用这 FooUserControl 。例如︰

<Window x:Class="OpenExcelFileAndConvertToArray.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:OpenExcelFileAndConvertToArray"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>        
    <StackPanel>            
        <ComboBox Text="qq" Name="comboBox">
            <ComboBoxItem Content="1"/>
            <ComboBoxItem Content="2"/>
            <ComboBoxItem Content="3"/>
        </ComboBox>
        <!--reusable control-->
        <local:FooUserControl/>            
    </StackPanel>
</Grid>

赞助商