Xamarin 表单,使用具有 INT 值的 OnPropertyChanged
Xamarin forms, working with a OnPropertyChanged with a INT value
我的 MasterDetailPage
上有一个按钮可以根据您点击的内容更改 INT(名为 App.value1)上的值,如下所示:
void click1 (object s, EventArgs a)
{
if (App.value1 == 0) {
App.value1 = App.value1 + 1;
} else {
App.value1 = 0;
}
}
而且我希望此点击功能立即更改我的 StartPage(另一个 ContentPage
)上的值。所以我创建了一个看起来像这样的视图模型,我尝试在其中使用当前值:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public int currentValue {
get {
return App.value1;
}
set {
if (App.value1 == 0) {
App.value1 = 0;
} else {
App.value1 = 1;
}
}
}
这是 StartPage
,我希望 INT 的值根据您在 MasterDetailView
上单击的内容立即更新。
public StartPage ()
{
var ourView = new StartPageViewModel ();
ourCurrentValue = ourView.currentValue;
}
protected async override void OnAppearing()
{
LoadData();
}
private async Task<List<Pin>> LoadData() //I work with pins here (not showing that code though as it is irrelavant.
{
if (ourCurrentValue == 0) {
System.Diagnostics.Debug.WriteLine ("Value is 0");
}
else {
System.Diagnostics.Debug.WriteLine ("Value is 1");
}
}
现在我在我的日志中只看到 "Value is 0"。当我点击 MasterDetailPage
.
上的按钮时,没有任何更新
更新代码:
public class StartPageViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public StartPageViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
和起始页:
public StartPage ()
{
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue"); // ERROR: `An object reference is requiered to access non-static member 'Xamarin.Forms.BindableObject.PropertyChanged`
}
您可以继续进行类似的操作:
对 class 中的 App
class 和 value1
属性 进行以下更改:
public static event PropertyChangedEventHandler PropertyChanged;
private static void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (null, new PropertyChangedEventArgs (propertyName));
}
}
private static int _value1;
public static int value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged("value1");
}
}
然后将此行添加到您的 StartPageViewModel
构造函数中:
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue");
在该代码中,您只是将 PropertyChanged
用于您自己的目的(您甚至可以为此创建自己的事件)。
我的意思是 StartPageViewModel
订阅了 App
class 中的 PropertyChanged
事件,所以当 value1
改变时它会被通知。当它实际发生时,它会调用自己的 PropertyChanged
来通知 View 关于 currentValue
的更改。
但是,我认为更好的解决方案是在 MasterDetailPage
和 StartPage
之间共享视图模型,因为使用全局状态会使您的解决方案难以理解:
public class SharedViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
/* INotifyPropertyChanged implementation */
public SharedViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
并且您需要在 MasterDetailPage
和 StartPage
中使用相同的 SharedViewModel
实例
我的 MasterDetailPage
上有一个按钮可以根据您点击的内容更改 INT(名为 App.value1)上的值,如下所示:
void click1 (object s, EventArgs a)
{
if (App.value1 == 0) {
App.value1 = App.value1 + 1;
} else {
App.value1 = 0;
}
}
而且我希望此点击功能立即更改我的 StartPage(另一个 ContentPage
)上的值。所以我创建了一个看起来像这样的视图模型,我尝试在其中使用当前值:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null) {
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public int currentValue {
get {
return App.value1;
}
set {
if (App.value1 == 0) {
App.value1 = 0;
} else {
App.value1 = 1;
}
}
}
这是 StartPage
,我希望 INT 的值根据您在 MasterDetailView
上单击的内容立即更新。
public StartPage ()
{
var ourView = new StartPageViewModel ();
ourCurrentValue = ourView.currentValue;
}
protected async override void OnAppearing()
{
LoadData();
}
private async Task<List<Pin>> LoadData() //I work with pins here (not showing that code though as it is irrelavant.
{
if (ourCurrentValue == 0) {
System.Diagnostics.Debug.WriteLine ("Value is 0");
}
else {
System.Diagnostics.Debug.WriteLine ("Value is 1");
}
}
现在我在我的日志中只看到 "Value is 0"。当我点击 MasterDetailPage
.
更新代码:
public class StartPageViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (this, new PropertyChangedEventArgs (propertyName));
}
}
public StartPageViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
和起始页:
public StartPage ()
{
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue"); // ERROR: `An object reference is requiered to access non-static member 'Xamarin.Forms.BindableObject.PropertyChanged`
}
您可以继续进行类似的操作:
对 class 中的 App
class 和 value1
属性 进行以下更改:
public static event PropertyChangedEventHandler PropertyChanged;
private static void OnPropertyChanged (string propertyName)
{
var changed = PropertyChanged;
if (changed != null)
{
PropertyChanged (null, new PropertyChangedEventArgs (propertyName));
}
}
private static int _value1;
public static int value1
{
get { return _value1; }
set
{
_value1 = value;
OnPropertyChanged("value1");
}
}
然后将此行添加到您的 StartPageViewModel
构造函数中:
App.PropertyChanged += (sender, args) => OnPropertyChanged("currentValue");
在该代码中,您只是将 PropertyChanged
用于您自己的目的(您甚至可以为此创建自己的事件)。
我的意思是 StartPageViewModel
订阅了 App
class 中的 PropertyChanged
事件,所以当 value1
改变时它会被通知。当它实际发生时,它会调用自己的 PropertyChanged
来通知 View 关于 currentValue
的更改。
但是,我认为更好的解决方案是在 MasterDetailPage
和 StartPage
之间共享视图模型,因为使用全局状态会使您的解决方案难以理解:
public class SharedViewModel : INotifyPropertyChanged
{
private ICommand clickCommand;
private int currentValue;
/* INotifyPropertyChanged implementation */
public SharedViewModel()
{
ClickCommand = new Command(() => CurrentValue = CurrentValue + 1);
}
public ICommand ClickCommand
{
get { return clickCommand; }
set
{
clickCommand = value;
OnPropertyChanged("ClickCommand");
}
}
public int CurrentValue
{
get { return currentValue; }
set
{
currentValue = value;
OnPropertyChanged("CurrentValue");
}
}
}
并且您需要在 MasterDetailPage
和 StartPage
SharedViewModel
实例