gpt4 book ai didi

c# - Xamarin - 将命令绑定(bind)到用户控件内对象的属性

转载 作者:太空狗 更新时间:2023-10-29 22:07:07 25 4
gpt4 key购买 nike

几天前我开始学习 XAML,但我很难解决这个问题。

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

我目前遇到异常:

Xamarin.Forms.Xaml.XamlParseException: 'Position 8:24. Cannot assign property "Test1": Property does not exists, or is not assignable, or mismatching type between value and property'

这是我目前正在尝试开始工作的简化版本。

我的自定义控件 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 进行控制

<?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>

我的自定义控件代码隐藏

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

}
}

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

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 并使其可绑定(bind),则需要遵循一些命名约定:

When you create the identifier field, name this field by the name of the property as you registered it, plus the suffix Property. This field is your identifier for the dependency property, and it will be used later as an input for the SetValue and GetValue calls you will make in the wrappers, by any other code access to the property by your own code, by any external code access you allow, by the property system, and potentially by XAML processors.

Define a DependencyProperty identifier as a public static readonly field on the owner type.

(这是 DependencyProperty 的文档,但在这种情况下它非常适用于 BindableProperties。参见 https://msdn.microsoft.com/en-us/library/ms753358(v=vs.110).aspx)

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

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

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

为了让你的全部工作正常,Button 命令绑定(bind)应该将它的源设置为控件本身,并且你应该将你的 MainPageViewModel 设置为 BindingContext for 主页.

关于c# - Xamarin - 将命令绑定(bind)到用户控件内对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753496/

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