我試圖讓以下內容在我的 .NET MAUI 專案中作業。
我有一個 view SettingView,它有一個BindableProperty字串型別。
internal class SettingView : ContentView
{
public static readonly BindableProperty BoundObjectPropery = BindableProperty.Create(
nameof(BoundObject),
typeof(string),
typeof(SettingView),
defaultValue: "",
propertyChanging: BoundObjectChanged,
defaultBindingMode: BindingMode.OneWay);
public string BoundObject
{
get => (string)GetValue(BoundObjectPropery);
set => SetValue(BoundObjectPropery, value);
}
}
我可以從 XAML 中呼叫它,如下所示:
<local:SettingView BoundObject="LiteralString" />
提供的 String 被傳遞并且可以從SettingView.
但是,當我嘗試使用 DataBinding 從視圖模型中傳遞屬性時,VS 拒絕構建并且出現錯誤
我在視圖模型中宣告了以下屬性
public string TestString => "Lorum ipsum";
XFC0009 沒有為“BoundObject”找到屬性、BindableProperty 或事件,或者值和屬性之間的型別不匹配
<local:SettingView BoundObject="{Binding TestString}" />
但是當我使用“原始”視圖時,比如標簽,它作業得很好..
<Label Text="{Binding TestString}"/>
如果我使用 Object 型別而不是 string 型別,它會構建并運行,并且我會收到一個“Binding”型別的物件。但似乎我無法對這個物件做任何有意義的事情。
Intellisense 確實抱怨說“沒有找到用于系結 'TestString' 的 DataContext”,但這不應該是一個問題,因為它在使用 MVVM 時在編譯時對 DataContext 一無所知。
誰能看到我做錯了什么?
uj5u.com熱心網友回復:
來自官方檔案的可系結屬性:創建屬性
可系結屬性的命名約定是可系結屬性識別符號必須與 Create 方法中指定的屬性名稱匹配,并附加“Property”。
您犯了一個錯字BoundObjectPropery,而不是BoundObjectProperty(BoundStringProper t y 相同),這就是您出現編譯錯誤的原因:
internal class SettingView : ContentView
{
public static readonly BindableProperty BoundObjectProperty = BindableProperty.Create(
nameof(BoundObject),
typeof(string),
typeof(SettingView),
defaultValue: "",
propertyChanging: BoundObjectChanged,
defaultBindingMode: BindingMode.OneWay);
public string BoundObject
{
get => (string)GetValue(BoundObjectProperty);
set => SetValue(BoundObjectProperty, value);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506688.html
