首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使项目中包含值,项目中的所有标签也会返回NULL

即使项目中包含值,项目中的所有标签也会返回NULL
EN

Stack Overflow用户
提问于 2020-04-29 08:31:47
回答 1查看 38关注 0票数 0

从今天开始,由于某些原因,我的所有标签都返回null。

尝试重新安装Visual studio,降级到随机版本的xamarin.form/essentials。在不同的PC上尝试相同的代码。同样的问题。

我的一个页面的代码:

XAML:

代码语言:javascript
运行
复制
    <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>

政务司司长:

代码语言:javascript
运行
复制
    public StorageDetailsPage()
        {
            InitializeComponent();

// checking if note contains nothing, if nothing returns N/A

            if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
            {
                lblnotes.Text = "N/A";
            }
        }

在我的开发过程中一切都很好,今天整个项目都搞砸了。

图片:

Debugging returns null

Commented out and shows not null in emulator

EN

回答 1

Stack Overflow用户

发布于 2020-04-29 09:14:42

原因是没有为ContentPage绑定数据。

代码语言:javascript
运行
复制
public StorageDetailsPage(object obj)
    {
        InitializeComponent();

        BindingContext = obj;  // not missing this in ContentPage         

        if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
        {
            lblnotes.Text = "N/A";
        }
    }

从第二个共享映像中,您将看到该非空。因为它设置了BindingContext = obj;,但是第一个镜像没有这样做。

修改代码如下以进行检查:

代码语言:javascript
运行
复制
public StorageDetailsPage()
    {
        InitializeComponent();

        BindingContext = new YourViewModel();  // bind a ViewModel here to check         
        if (String.IsNullOrWhiteSpace(lblnotes.Text) == true)
        {
            lblnotes.Text = "N/A";
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61492278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档