【问题标题】:Xamarin Forms XAML: yet another Binding syntax questionXamarin Forms XAML:又一个绑定语法问题
【发布时间】:2020-06-18 06:00:44
【问题描述】:

在我的 Xamarin Forms 项目中,我使用以下代码在设计时向 xaml 预览器提供模拟数据:

C#:

namespace MyApp.Data
{
    public class StaticData
    {
        private static AppMockData mockData;
        public static AppMockData MockData => mockData ?? (mockData = new MockData());

        public class MockData
        {
            public List<MyObj> LstObj { get; set; }
                = new List<MyObj>()
                {
                    new MyObj() { P1 = "V1", P2 = "V2" },
                    new MyObj() { P1 = "V1", P2 = "V2" },
                    new MyObj() { P1 = "V1", P2 = "V2" }
                };

            public MyObj SingleObj { get; set; } = new MyObj() { P1 = "V1", P2 = "V2" };
        }
    }
}

XAML:

<ContentPage xmlns:data="clr-namespace:MyApp.Data"
             BindingContext="{x:Static data:StaticData.MockData}"
             ... >  

<ListView x:Name="MyList" ItemsSource="{Binding LstObj}">
    ...
    <Label Text="{Binding P1}" />
    <Label Text="{Binding P2}" />
    ...
</ListView>

然后,在运行时:

var actualListData = GetDataFromDB(); // obj returned is of type List<MyObj>
MyList.ItemsSource = actualListData;

这工作正常。

我想对 SingleObj 做同样的事情,即单个对象,而不是列表。 我试过类似的东西:

<ContentPage xmlns:data="clr-namespace:MyApp.Data"
             BindingContext="{x:Static data:StaticData.MockData.SingleObj}"
             ... >

<Label Text="{Binding P1}" />
<Label Text="{Binding P2}" />

在后面的代码中:

var actualObj = GetDataFromDB(); // obj returned is of type MyObj
this.BindingSource = actualObj;

这在运行时有效,但在设计时无效:“{x:Static data:StaticData.MockData.SingleObj}”未被识别为有效表达式。

我可以让它在设计时和运行时都工作的唯一方法是:

<ContentPage xmlns:data="clr-namespace:MyApp.Data"
             BindingContext="{x:Static data:StaticData.MockData}"
             ... >

<StackLayout x:Name="MyStackLayout" BindingContext="{Binding SingleObj}">
    <Label Text="{Binding P1}" />
    <Label Text="{Binding P2}" />
</StackLayout>

和:

var actualObj = GetDataFromDB(); // obj returned is of type MyObj
MyStackLayout.BindingContext = actualObj;    

这样我就有了一个 StackLayout(仅用于绑定目的),它的 BindingContext 在设计时是 MockData 的属性,在运行时是实际对象。

不知道有没有语法直接在ContentPage BindingContext中指定SingleObj,从而避免把所有东西都放在一个容器布局中。

【问题讨论】:

标签: c# xaml xamarin.forms data-binding


【解决方案1】:

SingleObj 添加到您的绑定路径表达式

<ContentPage xmlns:data="clr-namespace:MyApp.Data"
             BindingContext="{x:Static data:StaticData.ViewModel}"
             ... >

<StackLayout>
    <Label Text="{Binding SingleObj.P1}" />
    <Label Text="{Binding SingleObj.P2}" />
</StackLayout>

【讨论】:

  • 这将在设计时工作,但我如何编码以在运行时显示实时数据?使用 StackLayout 我会执行“var actualObj = GetDataFromDB(); MyStackLayout.BindingSource = actualObj;”。你的方法不可能做到这一点......
  • 不要将 BindingContext 分配给您的 StackLayout,让它继承页面的上下文,一切都应该正常工作。
  • 对不起,但我不明白你将如何在运行时设置实际源...
  • this.BindingContext = new DataViewModel();
  • 我知道我对我的代码如何工作的解释不是很清楚。我重写了问题,希望更清楚
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 2018-09-28
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多