2017-01-19 38 views
2

前幾天我開始學習XAML,並且在解決這個問題時遇到了麻煩。Xamarin - 用戶控件內部對象的屬性綁定命令

在Xamarin Forms中,我想創建一個用戶控件,它將包含一個標籤和一個按鈕,並且能夠從使用我的用戶控件的另一頁面將命令綁定到XAML中的usercontrol。

我目前得到的異常:

Xamarin.Forms.Xaml.XamlParseException:「職位8:24。無法指定屬性「Test1」:屬性不存在,或者不可分配,或者值與屬性之間的配型不匹配'

這是一個虛擬版本,我目前正在努力工作。用我的自定義控件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); 
      } 
     } 

    } 
} 
使用自定義控制頁面的

視圖模型

我的自定義控制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> 

控制

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

回答

12

有一些命名約定,如果你希望你的BindableProperty檢測可循的,可綁定,在XAML:

當用戶通過屬性的名稱創建標識符字段名該字段爲您註冊它,加上後綴Property。該字段是依賴項屬性的標識符,稍後將用作SetValue和GetValue調用的輸入,您可以在包裝中進行SetValue和GetValue調用,也可以通過任何其他代碼訪問屬性(通過您自己的代碼),通過任何外部代碼訪問您允許,通過屬性系統,也可能通過XAML處理器。

定義DependencyProperty標識符作爲所有者類型公共靜態只讀字段。

(這是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); } 
} 

這應該防止屬性檢測問題。

讓你充滿東西的工作,按鈕命令結合應該有它的源設置爲控制本身,你應該設置你的MainPageViewModelBindingContextMainPage

0

非常感謝你們兩位,我已經和Krzysztof脫線了,在所有這些變化之後,還有一件事是讓它工作。

現在,當您單擊自定義控件中的按鈕(Control1)綁定的命令將不會被調用時,綁定中保存的命令爲空,但即使您通知視圖以從bindablecontext獲取新命令,也不會起作用,因爲它不會找到適當的屬性,因爲我們沒有指定bindablecontext,因此框架將在其他位置(在父對象中)搜索我們的屬性。不要嘗試將自定義控件的bindablecontext設置爲此,它將制動綁定到此控件的能力(您將無法正確綁定到這些依賴項屬性)。

在這種情況下解決它的最簡單方法是在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" 
x:Name="MySpecifiedName"> 
    <StackLayout> 
     <Button Command="{Binding Test1, Source={x:Reference MySpecifiedName}}" Text="Test"/> 
    </StackLayout> 
</ContentView> 

我已經挖如何解決它的話,我寫這裏保存其面臨的這些問題,其它的時間浪費了開發商幾個小時。

相關問題