Anonymous
Свойство привязки не найдено — MAUI .NET 8
Сообщение
Anonymous » 12 дек 2024, 22:30
Я работаю над проектом .NET MAUI и столкнулся со следующей ошибкой:
Код: Выделить всё
[0:] Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'IsAudioEnabled' property not found on 'TTIG02_Controller.Components.ToggleSettingsItem', target property: 'TTIG02_Controller.Components.ToggleSettingsItem.IsToggled'
[0:] Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'IsNotificationEnabled' property not found on 'TTIG02_Controller.Components.ToggleSettingsItem', target property: 'TTIG02_Controller.Components.ToggleSettingsItem.IsToggled'
Это соответствующие части кода:
SettingsPage.xaml :
SettingsPage.xaml.cs :
Код: Выделить всё
public partial class SettingsPage : ContentPage
{
public SettingsPage()
{
InitializeComponent();
BindingContext = Application.Current!.Handler.MauiContext!.Services.GetRequiredService();
}
private void ToggleSettingsItem_ToggleChanged(object sender, ToggledEventArgs e)
{
if (sender is not ToggleSettingsItem tsi) return;
if(string.IsNullOrWhiteSpace(tsi.Key)) return;
if(BindingContext is not SettingsPageViewModel vm) return;
Log.Verbose("SettingsPage.ToggleSettingsItem_ToggleChanged: {Key} toggled to {IsToggled}", tsi.Key, e.Value);
switch(tsi.Key)
{
case "audio":
vm.IsAudioEnabled = e.Value;
break;
case "notification":
vm.IsNotificationEnabled = e.Value;
break;
default:
Log.Warning("SettingsPage.ToggleSettingsItem_ToggleChanged: Unknown key {Key}", tsi.Key);
break;
}
}
}
ToggleSettingsItem.xaml :
ToggleSettingsItem.xaml.cs :
Код: Выделить всё
public partial class ToggleSettingsItem : ContentView
{
public string Key
{
get => (string)GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}
public string IconSource
{
get => (string)GetValue(IconSourceProperty);
set => SetValue(IconSourceProperty, value);
}
public string ItemText
{
get => (string)GetValue(ItemTextProperty);
set => SetValue(ItemTextProperty, value);
}
public bool IsToggled
{
get => (bool)GetValue(IsToggledProperty);
set => SetValue(IsToggledProperty, value);
}
public static readonly BindableProperty IconSourceProperty =
BindableProperty.Create(
propertyName: nameof(IconSource),
returnType: typeof(string),
declaringType: typeof(ToggleSettingsItem),
defaultValue: string.Empty,
defaultBindingMode: BindingMode.OneWay
);
public static readonly BindableProperty ItemTextProperty =
BindableProperty.Create(
propertyName: nameof(ItemText),
returnType: typeof(string),
declaringType: typeof(ToggleSettingsItem),
defaultValue: string.Empty,
defaultBindingMode: BindingMode.OneWay
);
public static readonly BindableProperty IsToggledProperty =
BindableProperty.Create(
propertyName: nameof(IsToggled),
returnType: typeof(bool),
declaringType: typeof(ToggleSettingsItem),
defaultValue: false,
defaultBindingMode: BindingMode.TwoWay
);
public static readonly BindableProperty KeyProperty =
BindableProperty.Create(
propertyName: nameof(Key),
returnType: typeof(string),
declaringType: typeof(ToggleSettingsItem),
defaultValue: string.Empty,
defaultBindingMode: BindingMode.OneWay
);
public event EventHandler? ToggleChanged;
public ToggleSettingsItem()
{
InitializeComponent();
ToggleSwitch.Toggled += OnToggleSwitchToggled;
}
private void OnToggleSwitchToggled(object? sender, ToggledEventArgs e)
{
Log.Verbose("ToggleSettingsItem.OnToggleSwitchToggled: {ItemText} toggled to {IsToggled}", ItemText, IsToggled);
ToggleChanged?.Invoke(this, e);
}
}
SettingsPageViewModel.cs :
Код: Выделить всё
namespace TTIG02_Controller.ViewModels
{
public class SettingsPageViewModel : INotifyPropertyChanged
{
private bool _isAudioEnabled;
public bool IsAudioEnabled
{
get => _isAudioEnabled;
set
{
if (_isAudioEnabled != value)
{
_isAudioEnabled = value;
OnPropertyChanged(nameof(IsAudioEnabled));
}
}
}
private bool _isNotificationEnabled;
public bool IsNotificationEnabled
{
get => _isNotificationEnabled;
set
{
if (_isNotificationEnabled != value)
{
_isNotificationEnabled = value;
OnPropertyChanged(nameof(IsNotificationEnabled));
}
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Следуя рекомендациям по похожим сообщениям, которые я уже пробовал:
Удалить x:DataType
Установить xmlns:local
Удалить BindingContext = this; из ToggleSettingsItem
< li>Doing IsToggled="{Binding" Source={x:Reference SettingsPage}, Path=BindingContext.IsNotificationEnabled}"
Может ли кто-нибудь помочь мне выяснить, что вызывает эту проблему?
Спасибо!
Подробнее здесь:
https://stackoverflow.com/questions/792 ... maui-net-8
1734031806
Anonymous
Я работаю над проектом .NET MAUI и столкнулся со следующей ошибкой: [code][0:] Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'IsAudioEnabled' property not found on 'TTIG02_Controller.Components.ToggleSettingsItem', target property: 'TTIG02_Controller.Components.ToggleSettingsItem.IsToggled' [0:] Microsoft.Maui.Controls.Xaml.Diagnostics.BindingDiagnostics: Warning: 'IsNotificationEnabled' property not found on 'TTIG02_Controller.Components.ToggleSettingsItem', target property: 'TTIG02_Controller.Components.ToggleSettingsItem.IsToggled' [/code] Это соответствующие части кода: [b]SettingsPage.xaml[/b]: [code] [/code] [b]SettingsPage.xaml.cs[/b]: [code]public partial class SettingsPage : ContentPage { public SettingsPage() { InitializeComponent(); BindingContext = Application.Current!.Handler.MauiContext!.Services.GetRequiredService(); } private void ToggleSettingsItem_ToggleChanged(object sender, ToggledEventArgs e) { if (sender is not ToggleSettingsItem tsi) return; if(string.IsNullOrWhiteSpace(tsi.Key)) return; if(BindingContext is not SettingsPageViewModel vm) return; Log.Verbose("SettingsPage.ToggleSettingsItem_ToggleChanged: {Key} toggled to {IsToggled}", tsi.Key, e.Value); switch(tsi.Key) { case "audio": vm.IsAudioEnabled = e.Value; break; case "notification": vm.IsNotificationEnabled = e.Value; break; default: Log.Warning("SettingsPage.ToggleSettingsItem_ToggleChanged: Unknown key {Key}", tsi.Key); break; } } } [/code] [b]ToggleSettingsItem.xaml[/b]: [code] [/code] [b]ToggleSettingsItem.xaml.cs[/b]: [code]public partial class ToggleSettingsItem : ContentView { public string Key { get => (string)GetValue(KeyProperty); set => SetValue(KeyProperty, value); } public string IconSource { get => (string)GetValue(IconSourceProperty); set => SetValue(IconSourceProperty, value); } public string ItemText { get => (string)GetValue(ItemTextProperty); set => SetValue(ItemTextProperty, value); } public bool IsToggled { get => (bool)GetValue(IsToggledProperty); set => SetValue(IsToggledProperty, value); } public static readonly BindableProperty IconSourceProperty = BindableProperty.Create( propertyName: nameof(IconSource), returnType: typeof(string), declaringType: typeof(ToggleSettingsItem), defaultValue: string.Empty, defaultBindingMode: BindingMode.OneWay ); public static readonly BindableProperty ItemTextProperty = BindableProperty.Create( propertyName: nameof(ItemText), returnType: typeof(string), declaringType: typeof(ToggleSettingsItem), defaultValue: string.Empty, defaultBindingMode: BindingMode.OneWay ); public static readonly BindableProperty IsToggledProperty = BindableProperty.Create( propertyName: nameof(IsToggled), returnType: typeof(bool), declaringType: typeof(ToggleSettingsItem), defaultValue: false, defaultBindingMode: BindingMode.TwoWay ); public static readonly BindableProperty KeyProperty = BindableProperty.Create( propertyName: nameof(Key), returnType: typeof(string), declaringType: typeof(ToggleSettingsItem), defaultValue: string.Empty, defaultBindingMode: BindingMode.OneWay ); public event EventHandler? ToggleChanged; public ToggleSettingsItem() { InitializeComponent(); ToggleSwitch.Toggled += OnToggleSwitchToggled; } private void OnToggleSwitchToggled(object? sender, ToggledEventArgs e) { Log.Verbose("ToggleSettingsItem.OnToggleSwitchToggled: {ItemText} toggled to {IsToggled}", ItemText, IsToggled); ToggleChanged?.Invoke(this, e); } } [/code] [b]SettingsPageViewModel.cs[/b]: [code]namespace TTIG02_Controller.ViewModels { public class SettingsPageViewModel : INotifyPropertyChanged { private bool _isAudioEnabled; public bool IsAudioEnabled { get => _isAudioEnabled; set { if (_isAudioEnabled != value) { _isAudioEnabled = value; OnPropertyChanged(nameof(IsAudioEnabled)); } } } private bool _isNotificationEnabled; public bool IsNotificationEnabled { get => _isNotificationEnabled; set { if (_isNotificationEnabled != value) { _isNotificationEnabled = value; OnPropertyChanged(nameof(IsNotificationEnabled)); } } } public event PropertyChangedEventHandler? PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } [/code] Следуя рекомендациям по похожим сообщениям, которые я уже пробовал: [list] [*]Удалить x:DataType [*]Установить xmlns:local [*]Удалить BindingContext = this; из ToggleSettingsItem < li>Doing IsToggled="{Binding" Source={x:Reference SettingsPage}, Path=BindingContext.IsNotificationEnabled}" [/list] Может ли кто-нибудь помочь мне выяснить, что вызывает эту проблему? Спасибо! Подробнее здесь: [url]https://stackoverflow.com/questions/79276049/binding-property-not-found-maui-net-8[/url]