.Net Maui 自定义输入字段未从视图模型传递值

问题描述 投票:0回答:1

我有一个自定义控件输入字段,当我在注册视图中使用此控件并传递一个值时,即使我输入了一个值,该值也将为空

自定义控件输入字段是

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BlogTemplate.ControlTemplates.ChicEntry">

    <ContentView.Resources>

        <ControlTemplate x:Key="ChicEntryControlTemplate">

            <Border 
                    StrokeThickness="0"
                    StrokeShape="RoundRectangle 10">

                <Entry Text="{Binding Text, Mode=TwoWay}" 
                       Placeholder="{Binding  Placeholder, Mode=TwoWay}" 
                       Margin="8,0,8,0"
                       BackgroundColor="{AppThemeBinding Light={StaticResource CardColorLight},
                            Dark={StaticResource CardColorDark}}"/>
                
            </Border>
    
        </ControlTemplate>
        
    </ContentView.Resources>
    
</ContentView>

带有c#后端

using static System.Net.Mime.MediaTypeNames;

namespace BlogTemplate.ControlTemplates;

public partial class ChicEntry : ContentView
{
    public static readonly BindableProperty TextProperty =
        BindableProperty.Create(nameof(Text), typeof(string), typeof(ChicEntry), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
        {
            var conttol = (ChicEntry)bindable;
            conttol.Text = newValue as string;
        });

    public static readonly BindableProperty PlaceholderProperty =
        BindableProperty.Create(nameof(Placeholder), typeof(string), typeof(ChicEntry), string.Empty);

    /// <summary>
    /// The string to be edited.
    /// </summary>
    public string Text
    {
        get => (string)GetValue(TextProperty);
        set => SetValue(TextProperty, value);
    }
    public string Placeholder
    {
        get => (string)GetValue(PlaceholderProperty);
        set => SetValue(PlaceholderProperty, value);
    }
    public ChicEntry()
    {
        InitializeComponent();
    }
}

注册视图模型

 public SignUpViewModel()
        {
            Title = AppResources.SignUp;
            SignUpCommand = new Command(SignUp);  
            // SignUpCommand = new Command(async () => await Shell.Current.GoToAsync($"../.."));
            LoginCommand = new Command(async () => await Shell.Current.GoToAsync(".."));
            //TermsCommand = new Command(async () => await Browser.OpenAsync("https://docs.microsoft.com/en-us/dotnet/maui/"));
        }

        private void SignUp(object obj)
        {
            var user = FulltName;
        }

注册视图

 <template:ChicEntry Text="{Binding Email, Mode=TwoWay}" 
                                    ControlTemplate="{StaticResource ChicEntryControlTemplate}"/>

当我单击注册按钮时,全名将为空,而我在输入字段中已经有一个值

.net maui
1个回答
0
投票

由于您将

Entry
控件放入
ControlTemplate
并将 Text 属性绑定到
ChicEntry
BindableProperty,我建议您使用 TemplateBinding

<Entry Text="{TemplateBinding Text, Mode=TwoWay}" 
       Placeholder="{Binding Placeholder, Mode=TwoWay}" 
       ...
       />

当在Entry中输入一些内容时,Text绑定的

BindableProperty
也会改变,ViewModel中的Property也会改变。

并且不需要注册属性改变的回调方法,

        //var conttol = (ChicEntry)bindable;
        //conttol.Text = newValue as string;

更多信息请参考使用TemplateBinding传递参数

© www.soinside.com 2019 - 2024. All rights reserved.