英文:
Value not binding in XAML Page in Xamarin.Forms
问题
以下是代码的中文翻译部分:
我有一个动态变化的文本,需要在用户界面中更新,但它没有绑定。
这是视图模型:
internal class PowerViewModel : INotifyPropertyChanged
{
public string Power { get; set; }
public PowerViewModel()
{
MessagingCenter.Instance.Subscribe<App, string>(this, "power_topic", OnPowerChanged);
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPowerChanged(App app, string power)
{
Power = power;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Power)));
}
}
OnPowerChanged()
方法被触发,并且 Power
变量获得了一个值。
视图代码和绑定如下:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
BindingContext = new ItemDetailViewModel();
}
}
}
以下是XAML代码:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:IotApp.ViewModels" x:DataType="viewmodels:PowerViewModel"
x:Class="IotApp.Views.ItemDetailPage">
<ContentPage.BindingContext>
<viewmodels:PowerViewModel />
</ContentPage.BindingContext>
<ContentPage.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#faeee2" Offset="0.1"/>
<GradientStop Color="#a0bec6" Offset="0.5"/>
</LinearGradientBrush>
</ContentPage.Background>
<Shell.TitleView>
<StackLayout Orientation="Horizontal">
<Label Text="Home" FontSize="16"/>
<Image Source="logo_white" VerticalOptions="EndAndExpand" HorizontalOptions="EndAndExpand"/>
</StackLayout>
</Shell.TitleView>
<StackLayout Spacing="20" Padding="15" VerticalOptions="Start">
<Label Text="{Binding Power}" FontSize="18" TextColor="#1c2d57" />
</StackLayout>
</ContentPage>
它应该显示 Power
的值,但始终为空,我无法弄清楚为什么。 请帮助。
非常感谢。
英文:
I have a text that is changing dynamically and needs to be updated in user interface, but it is not binding.
Here is the view model:
internal class PowerViewModel : INotifyPropertyChanged
{
public string Power { get; set; }
public PowerViewModel()
{
MessagingCenter.Instance.Subscribe<App, string>(this, "power_topic", OnPowerChanged);
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPowerChanged(App app, string power)
{
Power = power;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Power)));
}
}
the method OnPowerChanged()
is being triggered and Power
is taking a value.
The view code and binding is this:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
BindingContext = new ItemDetailViewModel();
}
}
}
And here is the xaml code:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:IotApp.ViewModels" x:DataType="viewmodels:PowerViewModel"
x:Class="IotApp.Views.ItemDetailPage">
<ContentPage.BindingContext>
<viewmodels:PowerViewModel />
</ContentPage.BindingContext>
<ContentPage.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#faeee2" Offset="0.1"/>
<GradientStop Color="#a0bec6" Offset="0.5"/>
</LinearGradientBrush>
</ContentPage.Background>
<Shell.TitleView>
<StackLayout Orientation="Horizontal">
<Label Text="Home" FontSize="16"/>
<Image Source="logo_white" VerticalOptions="EndAndExpand" HorizontalOptions="EndAndExpand"/>
</StackLayout>
</Shell.TitleView>
<StackLayout Spacing="20" Padding="15" VerticalOptions="Start">
<Label Text="{Binding Power}" FontSize="18" TextColor="#1c2d57" />
</StackLayout>
</ContentPage>
It should show the value of Power
, but is empty all the time, I just can't figure out why.
Please, help.
Thank you very much.
答案1
得分: 1
问题
问题在于您多次设置了BindingContext
。
您在这里设置了两次:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
BindingContext = new ItemDetailViewModel();
}
}
}
并且在这里设置了第三次:
<ContentPage.BindingContext>
<viewmodels:PowerViewModel />
</ContentPage.BindingContext>
代码后端中的第二次分配具有优先权,因此您实际上尝试绑定到错误的ViewModel。
解决方案
在XAML中删除BindingContext
的分配,并且还要删除代码后端中的第二次分配,以便代码如下所示:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
}
}
}
英文:
Problem
The problem is that you're setting the BindingContext
multiple times.
You set it twice here:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
BindingContext = new ItemDetailViewModel();
}
}
}
And a third time here:
<ContentPage.BindingContext>
<viewmodels:PowerViewModel />
</ContentPage.BindingContext>
The second assignment in the code-behind takes precedence, so you're effectively trying to bind to the wrong ViewModel.
Solution
Remove the assignment of the BindingContext
in the XAML and also remove the second assignment in the code behind, so that the code looks like this:
namespace IotApp.Views
{
public partial class ItemDetailPage : ContentPage
{
public ItemDetailPage()
{
InitializeComponent();
BindingContext = new PowerViewModel();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论