【问题标题】:Xamarin XAML Previewer Design Data BindingsXamarin XAML 预览器设计数据绑定
【发布时间】: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";

            }
        }
    }
}

【问题讨论】:

标签: xaml xamarin.forms


【解决方案1】:
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


        }

【讨论】:

  • 我试过这个,但它不起作用。我不打算更改该属性 - 它意味着要在设计时使用的通用固定数据集,因此基本上我可以显示此绑定中的 dat。我将其用作测试,因此我可以将其用于 ItemSource(否则我会从 Web API 中提取),因此实现的标准视图模型将处理此问题。
【解决方案2】:

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

来自

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

            }
        }
}

这应该可以解决您的问题

【讨论】:

  • 不,这没有用 - 仍然没有显示绑定数据。
  • 你的 TimerViewModel 有带参数的构造函数吗?当您运行您的应用程序时会发生什么,您是否看到绑定到控件的值/结果?
猜你喜欢
  • 1970-01-01
  • 2021-06-22
  • 2019-01-02
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多