Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在行为中绑定属性?_C#_Xaml_Xamarin.forms - Fatal编程技术网

C# 如何在行为中绑定属性?

C# 如何在行为中绑定属性?,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我创建了一个使用电话掩码的行为。当显式插入掩码时,它工作正常。但是,当我在选择器中更改电话类型时,我希望与视图模型通信以更改掩码 在xaml文件中: <Picker x:Name="picker_cadastronotificacao_tipotelefone" Title="{x:Static local:AppResources.picker_cadastronotificacao_tipotelefone}" Style="{StaticRes

我创建了一个使用电话掩码的行为。当显式插入掩码时,它工作正常。但是,当我在选择器中更改电话类型时,我希望与视图模型通信以更改掩码

在xaml文件中:

<Picker 
      x:Name="picker_cadastronotificacao_tipotelefone" 
      Title="{x:Static local:AppResources.picker_cadastronotificacao_tipotelefone}"
      Style="{StaticResource Picker}"
      SelectedItem="{Binding TipoTelefoneSelecionado}">
      <Picker.Items>
             <x:String>Celular</x:String>
             <x:String>Telefone Fixo</x:String>
      </Picker.Items>
</Picker>


<Entry 
      x:Name="entry_cadastronotificacao_telefone" 
      Placeholder="{x:Static local:AppResources.entry_cadastronotificacao_telefone}" 
      Style="{StaticResource EntradasTexto}" 
      Text="{Binding Telefone}"
      Keyboard="Telephone"
      IsEnabled="{Binding HabilitarCampoTelefone}">

      <Entry.Behaviors>
             <behavior:MaskBehavior Mascara="{Binding MascaraTelefone}"/>
      </Entry.Behaviors>
</Entry>

错误:找不到“Mascara”的属性、可绑定属性或事件,或者值和属性之间的类型不匹配。

您需要将
MaskBehavior.Mascara
属性设置为可绑定属性(与“常规”属性相反)

也就是说,在
MaskBehavior
中,而不是在此:

public string Mascara { get; set; }
这样做:

public static readonly BindableProperty MascaraProperty = BindableProperty.Create(
                nameof(Mascara),
                typeof(MaskBehavior),
                typeof(string));

public string Mascara
{
    get => (string) GetValue(MascaraProperty);
    set => SetValue(MascaraProperty, value);
}
public static readonly BindableProperty MascaraProperty = BindableProperty.Create(
                nameof(Mascara),
                typeof(MaskBehavior),
                typeof(string));

public string Mascara
{
    get => (string) GetValue(MascaraProperty);
    set => SetValue(MascaraProperty, value);
}