从今天开始,由于某些原因,我的所有标签都返回null。
尝试重新安装Visual studio,降级到随机版本的xamarin.form/essentials。在不同的PC上尝试相同的代码。同样的问题。
我的一个页面的代码:
XAML:
<ScrollView x:Name="ScrollView">
<StackLayout Spacing="20" Padding="20">
<Label Text="Drugs Name:" FontSize="Small" />
<Label Text="{Binding DrugsName}" d:Text="Drug Name" FontSize="Medium"/>
<Label Text="Nick Name:" FontSize="Small" />
<Label Text="{Binding NickName}" d:Text="Drug Nick Name" FontSize="Medium"/>
<Label Text="Expiry Date:" FontSize="Small" />
<Label Text="{Binding ExpiryDate, StringFormat='{0:MMMM dd,yyyy}'}" d:Text="Quantity" FontSize="Medium"/>
<Label Text="Note:" FontSize="Small"/>
Note Label ----> <Label Text="{Binding Note}" x:Name="lblnotes" d:Text="Note" FontSize="Medium"/>
<Label Text="Location Image:" FontSize="Small"/>
<Image Source="{Binding image_path}"/>
</StackLayout>
</ScrollView>政务司司长:
public StorageDetailsPage()
{
InitializeComponent();
// checking if note contains nothing, if nothing returns N/A
if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
{
lblnotes.Text = "N/A";
}
}在我的开发过程中一切都很好,今天整个项目都搞砸了。
图片:
发布于 2020-04-29 09:14:42
原因是没有为ContentPage绑定数据。
public StorageDetailsPage(object obj)
{
InitializeComponent();
BindingContext = obj; // not missing this in ContentPage
if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
{
lblnotes.Text = "N/A";
}
}从第二个共享映像中,您将看到该非空。因为它设置了BindingContext = obj;,但是第一个镜像没有这样做。
修改代码如下以进行检查:
public StorageDetailsPage()
{
InitializeComponent();
BindingContext = new YourViewModel(); // bind a ViewModel here to check
if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
{
lblnotes.Text = "N/A";
}
}https://stackoverflow.com/questions/61492278
复制相似问题