小编Krz*_*ona的帖子

Xamarin - 绑定命令到User Control内对象的属性

我几天前就开始学习XAML了,我遇到了解决这个问题的麻烦.

在Xamarin Forms中,我想创建一个用户控件,它将包含一个标签和一个按钮,并能够将命令绑定到XAML中的usercontrol来自另一个使用我的用户控件的页面.

我目前正在例外:

Xamarin.Forms.Xaml.XamlParseException:'位置8:24.无法分配属性"Test1":属性不存在,或者不可分配,或者值和属性之间的类型不匹配'

这是我目前正在努力工作的一个愚蠢的版本.

我的自定义控件XAML

<?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="App3.Controls.Control1">
    <StackLayout>
        <Button Command="{Binding Test1}" Text="Test"/>
    </StackLayout>
</ContentView>
Run Code Online (Sandbox Code Playgroud)

使用我的自定义控件XAML进行控制

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App3"
             xmlns:controls="clr-namespace:App3.Controls"
             x:Class="App3.MainPage">

    <controls:Control1 Test1="{Binding Test2}" />
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

我的自定义控件代码隐藏

using System.Windows.Input;
using Xamarin.Forms;

namespace App3.Controls
{
    public partial class Control1 : ContentView
    {

        private static readonly BindableProperty controlProperty = BindableProperty.Create("Test1", typeof(ICommand), typeof(Control1), null);

        public Control1()
        {
            InitializeComponent();
        }

        public ICommand Test1
        {
            get
            {
                return (ICommand)GetValue(controlProperty);
            }
            set
            {
                SetValue(controlProperty, value); …
Run Code Online (Sandbox Code Playgroud)

c# xaml xamarin xamarin.forms

5
推荐指数
2
解决办法
5025
查看次数

标签 统计

c# ×1

xamarin ×1

xamarin.forms ×1

xaml ×1