如何在Xamarin.Forms中使用MVVM创建UserControl

如何在Xamarin.Forms中使用MVVM创建UserControl,xamarin,mvvm,xamarin.forms,Xamarin,Mvvm,Xamarin.forms,我曾尝试在Xamarin.Forms中开发一个usercontrol,但无法使用ViewModel绑定该usercontrol的控件 我的CustomeentrySerControl看起来像 <?xml version="1.0" encoding="UTF-8"?> <Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/x

我曾尝试在Xamarin.Forms中开发一个usercontrol,但无法使用ViewModel绑定该usercontrol的控件

我的CustomeentrySerControl看起来像

    <?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LearningApp.UserControls.CustomeEntry"
             xmlns:local="clr-namespace:LearningApp.Extension">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="20" />
    </Grid.ColumnDefinitions>
    <Entry Grid.Column="0"  x:Name="entry" />
    <local:RoundedButton Grid.Column="1" Text="X" BackgroundColor="Gray" TextColor="White" HeightRequest="20" WidthRequest="10" VerticalOptions="Center" Margin="-30,0,30,0" x:Name="cancelbtn" />

</Grid>
 public partial class CustomeEntry : Grid
{
    public CustomeEntry()
    {
        InitializeComponent();

    }

    public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(CustomeEntry), default(string));

    public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(CustomeEntry), typeof(ICommand), default(ICommand));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public string PlaceHolder
    {
        get { return entry.Placeholder; }
        set { entry.Placeholder = value; }
    }
}
我曾尝试在我的一个内容页中使用此控件,如下所示

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="LearningApp.Views.TriggersDemo.DataTriggers"
             xmlns:local="clr-namespace:LearningApp.UserControls">
    <StackLayout Orientation="Vertical">
        <Label FontSize="Large" FontAttributes="Bold" Text="{Binding lblText}"></Label>
        <local:CustomeEntry   Text="{Binding Name}" Command="{Binding CancelCommand}" PlaceHolder="Enter Name"/>
        <Button Command="{Binding CheckCommand}" Text="Check" />

    </StackLayout>
</ContentPage>
我的ViewModel包含一个属性,如

 private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; RaisePropertyChanged(); }
        }

任何人都可以指导我如何将UserControl的条目
Text
绑定到我的ViewModel属性
Name
direct吗?

在您的
CustomerEntry
中没有显示您绑定到的文本的控件

您还必须在自定义控件中设置所有绑定

您还可以将处理程序传递给
TextProperty
的参数,并代表设置其他视图的属性

从自定义控件中删除属性,并直接绑定到将要传递的
BindingContext
(包含您的viewmodel)。

然后在自定义控件的条目中设置
Text=“{Binding Text=。}”
(与
按钮的
命令相同),使其绑定到
绑定上下文中的文本,实际上您可以从自定义控件中删除所有属性,并直接绑定到ViewModel。

是,但是如何从我使用usercontrol的ContentPage绑定usercontrol条目呢
 private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; RaisePropertyChanged(); }
        }