【发布时间】:2019-07-17 08:13:32
【问题描述】:
我正在尝试在 Xamarin XAML 页面中设置设计时数据绑定,以便我可以利用预览器更快地设计我的页面。我已经设置了一个包含我的数据的静态类,但是当我预览它时,它并没有显示绑定的数据。
如何让绑定工作?我正在尝试使用 Test 属性
XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:TechsportiseApp.Data;assembly=DesignTimeData"
BindingContext="{x:Static local:DesignTimeData.ViewModel}"
x:Class="TechsportiseApp.Views.Timer" x:Name="ParentView" Title="Timer">
<Entry Margin = "3" Text="{Binding Test}" />
</ContentPage>
DesignTimeData.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using TechsportiseApp.Models;
namespace TechsportiseApp.Data
{
public class DesignTimeData
{
public static class ViewModelLocator
{
private static TimerViewModel timerVM;
public static TimerViewModel ViewModel => timerVM ?? (timerVM = new TimerViewModel());
public class TimerViewModel
{
public ObservableCollection<Timing> Timings { get; set; } = new ObservableCollection<Timing>
{
new Timing { Position = 1, BatchCode = "A", Elapsed = "01:02:03.001", EndTime = DateTime.Now, StartTime = DateTime.Now, Id = 1, RaceId = 111 }
};
public List<RaceOption> RaceOptions { get; set; } = new List<RaceOption>
{
new RaceOption { Id = 1, RaceId = 2, Colour= "Red", OptionName="5k" },
new RaceOption { Id = 3, RaceId = 4, Colour= "Blue", OptionName="10k" },
new RaceOption { Id = 5, RaceId = 6, Colour= "Green", OptionName="15k" },
new RaceOption { Id = 5, RaceId = 8, Colour= "Yello", OptionName="20k" },
};
public string Test = "Bind Test";
}
}
}
}
【问题讨论】:
-
我已经看过了,我按照下面链接的指南,还看了 Montemagno 的博客。最好我能说这是对的,但我显然错了,否则我不会发布xamarinhelp.com/xamarin-forms-previewer
-
Test 应该是您的视图模型/绑定上下文中实现 INotifyOnPropertyChanged 的公共属性。
-
如果你保持简单,it would work...:O)
-
有趣的@jsanalytics,但我怎样才能让它变得简单?
标签: xaml xamarin.forms