本文介绍了Xamarin-将命令绑定到用户控件内对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我已经开始学习XAML,但是在解决这个问题时遇到了麻烦.

I have began learning XAML a few days ago and I have a trouble with wrapping my head around this problem.

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

In Xamarin Forms, I want to create a user control which will contain a label and a button and be able to bind a command to usercontrol in XAML from another page which uses my user control.

我目前遇到异常:

这是我目前想开始使用的精简版本.

Here is a dumbed down version of it I am currently trying to get to work.

我的自定义控件XAML

My Custom Control 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>

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

Control using my Custom Control 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>

隐藏了我的自定义控制代码

My Custom Control Codebehind

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);
            }
        }

    }
}

使用自定义控件"查看页面模型

View Model of page using Custom Control

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Xamarin.Forms;

namespace App3
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public MainPageViewModel()
        {
            this.Test2 = new Command(test);
        }

        public void test()
        {
            Application.Current.MainPage.DisplayAlert("it works", "it works", "it works");
        }

        public ICommand Test2
        {
            get; set;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

推荐答案

如果要从Xaml检测到可绑定的BindableProperty,可以遵循以下命名约定:

There are a few naming convention to follow if you want your BindableProperty detected, and bindable, from Xaml:

(这是DependencyProperty的文档,但是在这种情况下,它非常适用于BindableProperties.请参见)

(this is the documentation for DependencyProperty, but it applies to BindableProperties quite well in this case. See https://msdn.microsoft.com/en-us/library/ms753358(v=vs.110).aspx)

您可以通过替换来修复代码

You can fix your code by replacing

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

public ICommand Test1
{
    get { return (ICommand)GetValue(controlProperty); }
    set { SetValue(controlProperty, value); }
}

作者

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


public ICommand Test1
{
    get { return (ICommand)GetValue(Test1Property); }
    set { SetValue(Test1Property, value); }
}

这应该可以防止属性检测问题.

That should prevent the property detection issue.

为使您的全部内容正常工作,Button命令绑定应将其源设置为控件本身,并且应将MainPageViewModel设置为BindingContext作为MainPage.

to get your full stuff working, the Button command binding should have it's source set to the control itself, and you should set your MainPageViewModel as BindingContext for MainPage.

这篇关于Xamarin-将命令绑定到用户控件内对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:30
查看更多