数据绑定未使用OnPropertyChanged更新

时间:2019-07-10 23:04:47

标签: c# xaml xamarin xamarin.forms xamarin.android

使用Xamarian.Forms构建我的第一个应用程序。我的基本菜单和主页都带有一些标签和一个按钮,以便可以绑定一些数据和方法,并确保游戏的逻辑代码可以正常工作。我使数据绑定在标签作用下一直有效,它们出现在屏幕上。但是我不确定我的绑定是否没有更新,或者我的命令是否没有绑定。因此,我注释掉了ICommand,删除了绑定,并放入了将方法推进到xaml背后的代码中的方法。即使在此之后,单击按钮时数据也不会更新,这使我认为OnPropertyChanged和数据绑定存在问题。我搜索了网络和相关问题,实现了几种不同的方式来编写绑定的获取和设置,以几种不同的方式编写了我的OnPropertyChanged函数,但是当单击主页上的工具栏按钮时仍然没有任何反应

这是我的HomePageViewModel.cs,其中包含INotifyPropertyChanged

***经过编辑以反映自获取日值更新以来所做的更改:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using Engine;
using Xamarin.Forms;

namespace TestApp
{
    public class HomePageViewModel : INotifyPropertyChanged
    {
        public static Player _player = World.Player1;

        public string Day = World.TrueDay.ToString();

        public string MoneyValue = Convert.ToInt32(Math.Floor(World.Player1.PlayerMoney)).ToString();

        public string CurrentLocation = _player.CurrentLocation.Name;


        public HomePageViewModel()
        {
            OnTurn = new Command(execute: On1Turn);
        }

        public ICommand OnTurn { get; private set; }

        public string CurrentDay
        {

            get { return Day; }

            set { Day = value; OnPropertyChanged(); }

        }

        public string Money
        {

            get { return MoneyValue; }

            set { MoneyValue = value; OnPropertyChanged(); }
        }

        public string PlayerLocation
        {

            get { return CurrentLocation; }

            set { CurrentLocation = value; OnPropertyChanged(); }
        }

        void On1Turn()
        {

            World.TrueDay = World.TrueDay + 1;

            CurrentDay = World.TrueDay.ToString();

            World.Player1.PlayerMoney = World.Player1.PlayerMoney + 1000;

            MoneyValue = Convert.ToInt32(Math.Floor(World.Player1.PlayerMoney)).ToString();

            OnPropertyChanged(Money);

        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }    
    }
}

这是我的HomePage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local1="clr-namespace:TestApp"
             mc:Ignorable="d"
             x:Class="TestApp.HomePage">

    <ContentPage.BindingContext>
        <local1:HomePageViewModel/>
    </ContentPage.BindingContext>

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="+24hrs" Clicked="ToolbarItem_Clicked"  />
    </ContentPage.ToolbarItems>


    <StackLayout Padding="20">

        <StackLayout Orientation="Horizontal">
            <Label Text="Money:" HorizontalOptions="Start"/>
            <Label x:Name="lblPlayerMoney"  Text="{Binding Money, Mode=OneWay}" HorizontalOptions="FillAndExpand"/>
        </StackLayout>

        <StackLayout Orientation="Horizontal" VerticalOptions="EndAndExpand" Margin="0,-40,0,0">
            <Label Text="Current Location:" HorizontalOptions="CenterAndExpand"/>
            <Label x:Name="lblPlayerLocation" Text="{Binding PlayerLocation, Mode=OneWay}"  HorizontalOptions="CenterAndExpand"/>
        </StackLayout>

        <StackLayout Orientation="Horizontal" VerticalOptions="StartAndExpand">
            <Label Text="Current Day:" HorizontalOptions="CenterAndExpand" Margin="30,0,0,0"/>
            <Label x:Name="lblCurrentDay" Text="{Binding CurrentDay, Mode=OneWay}" HorizontalOptions="CenterAndExpand"/>
        </StackLayout>

    </StackLayout>




</ContentPage>

还有HomePage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace TestApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {
        public HomePage()
        {
            InitializeComponent();

            //BindingContext = new HomePageViewModel();
        }

        private void ToolbarItem_Clicked(object sender, EventArgs e)
        {
            World.TrueDay = World.TrueDay + 1;

            World.Player1.PlayerMoney = World.Player1.PlayerMoney + 1000;
        }
    }
}

由于我是Xamarian.Forms和OnPropertyChanged功能的新手,因此对该问题的任何见解将不胜感激。感谢您的时间!

编辑******

这是World.cs,如果我设置了属性,是否可以帮助

using System;
using System.Collections.Generic;
using System.Text;

namespace Engine
{
    public class World
    {
        public static decimal TrueDay = 1;
        //public string LocationText = Player1.CurrentLocation.Name.ToString();

        public static Player Player1;

        public static readonly List<Location> Locations = new List<Location>();

        public const int LOCATION_ID_OSHAWA = 1;
        public const int LOCATION_ID_TORONTO = 2;

        public static void GenerateWorld()
        {
            PopulateLocations();
            Player1 = new Player("Jordan", LocationByID(LOCATION_ID_OSHAWA), 5000);
        }
        private static void PopulateLocations()
        {
            Location oshawa = new Location(LOCATION_ID_OSHAWA, "Oshawa");
            Location toronto = new Location(LOCATION_ID_TORONTO, "Toronto");

            Locations.Add(oshawa);
            Locations.Add(toronto);
        }
        public static Location LocationByID(int id)
        {
            foreach (Location location in Locations)
            {
                if (location.ID == id)
                {
                    return location;
                }
            }

            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不太了解项目逻辑,但是如果要更改模型数据,可以执行以下操作

HomePage.xaml.cs

namespace TestApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class HomePage : ContentPage
    {
        HomePageViewModel homePageViewModel = new HomePageViewModel();
        public HomePage()
        {
            InitializeComponent();

            BindingContext = homePageViewModel ;
        }

        private void ToolbarItem_Clicked(object sender, EventArgs e)
        {
            homePageViewModel.CurrentDay = xxx ;

            homePageViewModel.xxxxx = xxxx;
            //Something like this can change model data
        }
    }
}

这里是可以参考的sample数据绑定讨论。