Xamarin XAML预览器设计数据绑定

时间:2019-07-17 08:13:32

标签: xaml xamarin.forms

我试图在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";

            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

public class TimerViewModel
        {
        private string _test = "Bind Test";

            public string Test
            {
                get
                {
                    return _test;
                }
                set
                {
                    value = _test;
                    //implement your INotifyOnPropertyChangedHere...
                }
            }

            /// the rest of your view model code


        }

答案 1 :(得分:0)

您似乎没有正确定义您的课程。更改以下结构

来自

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";

            }
        }
    }
}

收件人

namespace TechsportiseApp.Data
{
    public class DesignTimeData
    {
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";

            }
        }
}

这应该可以解决您的问题