gpt4 book ai didi

c# - 如何制作组件,然后在 XAML 中重用?

转载 作者:太空狗 更新时间:2023-10-29 23:29:24 26 4
gpt4 key购买 nike

我决定先做控件再重用。作为 Angular 指令。但只到达了广告。

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>

成功抓到


target invocation exception


我做错了什么?

最佳答案

要创建可重复使用的控件,您应该创建 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>

关于c# - 如何制作组件,然后在 XAML 中重用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37206293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com