简体   繁体   English

如何使用 c# 将单个 html 行绑定到 xamarin.forms 中的多个标签

[英]How to bind individual html lines to multiple labels in xamarin.forms with c#

I've just started using binding so this may simple but I haven't found any suitable answers online.我刚刚开始使用绑定,所以这可能很简单,但我还没有在网上找到任何合适的答案。 I have the labels我有标签

 <Label x:DataType="myNameSpace:MyClass" Text = "{Binding label}"/> <Label x:DataType="myNameSpace:MyClass" Text = "{Binding label2}"/>

I need to bind different strings of html from json elements stored in a dynamic dynamicObject;我需要从存储在dynamic dynamicObject; to different labels.到不同的标签。 So I can't just assign the entire view to a label as Label Class Doc suggests.所以我不能像Label Class Doc建议的那样将整个视图分配给 label。 The C# is C# 是

        Label label = new Label();
        label.Text = dynamicObject.mainText;
        label.TextType = TextType.Html;

However, I need to use xaml and it always says that label is not found in the data context MyClass.但是,我需要使用 xaml,它总是说在数据上下文 MyClass 中找不到label Even using public String label{get{return "Text";}} does not display anything.即使使用public String label{get{return "Text";}}也不会显示任何内容。 What is the most efficient way to do access different strings from the object and display them in different labels?从 object 访问不同字符串并将它们显示在不同标签中的最有效方法是什么?

{Binding label} requires that the BindingContext contain a "public property" named label . {Binding label}要求BindingContext包含一个名为label的“公共属性”。

Do you SET BindingContext anywhere?你在任何地方设置 BindingContext 吗? In view constructor, do在视图构造函数中,执行

BindingContext=new MyClass(); . .

... modify as needed, to initialize MyClass with whatever data it should have ...根据需要进行修改,以使用它应该具有的任何数据初始化 MyClass


Then MyClass.cs must look something like this:那么 MyClass.cs 必须看起来像这样:

public class MyClass
{
    // These must be PUBLIC PROPERTIES.
    public string label { get; set; }
    public string label2 { get; set; }
}

Usage looks like this:用法如下所示:

<Label Text = "{Binding label}"/>
<Label Text = "{Binding label2}"/>

Note that, as Jason says, there are no x:DataType attributes.请注意,正如 Jason 所说,没有x:DataType属性。 Those are for a different situation.这些是针对不同情况的。


A common mistake is to not have PROPERTIES.一个常见的错误是没有属性。

// This won't work. This is a FIELD, not a property.
public string label;

If you still can't run because of errors that refer to DataContext or DataType, then:如果由于引用 DataContext 或 DataType 的错误仍然无法运行,则:

  • REMOVE ALL x:DataType statements in the xaml file.删除 xaml 文件中的所有x:DataType语句。 There is probably one in the first few lines of the XAML. XAML 的前几行中可能有一个。 <-- ONLY REMOVE IF this is preventing the app from building or running successfully. <-- 仅当这会阻止应用程序成功构建或运行时才删除。 If it isn't causing any fatal problem, leave it there.如果它没有导致任何致命问题,请将其留在那里。

  • IGNORE (disregard) any DataType/DataContext warning from Intellisense - just run it, and see if it works.忽略(忽略)来自 Intellisense 的任何 DataType/DataContext 警告 - 只需运行它,看看它是否有效。

If you get that to work, then it is possible to start adding x:DataType statements back in, to eliminate the intellisense warnings.如果你让它工作,那么可以开始重新添加x:DataType语句,以消除智能感知警告。 But they don't belong on the individual labels.但它们不属于单独的标签。 I consider this an "advanced" topic, and won't say more about it here.我认为这是一个“高级”主题,这里不再赘述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM