gpt4 book ai didi

android - 我的 StateButton 状态没有更新我的 View

转载 作者:行者123 更新时间:2023-11-28 23:28:26 26 4
gpt4 key购买 nike

我正在尝试使用 Xamarin.CustomControls 中的 StateButtons

我有 2 个状态按钮,我希望它们以二分法的方式工作。

我的 View 模型是这样的:

        private bool _btn1Active { get; set; }

public bool Btn1Active
{
get
{
return _btn1Active;
}
set
{
if(_btn1Active != value)
{
_btn1Active = value;

NotifyPropertyChanged("Btn1Active");
}
}
}

private bool _btn2Active { get; set; }

public bool Btn2Active
{
get
{
return _btn2Active;
}
set
{
if(_btn2Active != value)
{
_btn2Active = value;

NotifyPropertyChanged("Btn2Active");
}
}
}

我已经像这样绑定(bind)了状态按钮:

Btn1.SetBinding(StateButton.IsPressedProperty, "Btn1Active");
Btn2.SetBinding(StateButton.IsPressedProperty, "Btn2Active");

我的 button_clicked 处理程序是:


private void Btn_Clicked(object sender, EventArgs e)
{
var myViewModel = BindingContext as BtnsViewModel;

myViewModel.Btn1Active= !myViewModel.Btn1Active;
myViewModel.Btn2Active= !myViewModel.Btn2Active;

}

我已经调试了很长时间,我看到每个属性都得到通知并正确更改,但 View 没有更新。

最佳答案

我创建了一个演示来测试您的代码,它工作正常。主要代码如下:

  public class TestModel2: INotifyPropertyChanged
{
public TestModel2()
{
Btn1Active = false;
Btn2Active = false;
}

private bool _btn1Active;
public bool Btn1Active
{
set { SetProperty(ref _btn1Active, value); }
get { return _btn1Active; }
}

private bool _btn2Active;
public bool Btn2Active
{
set { SetProperty(ref _btn2Active, value); }
get { return _btn2Active; }
}



bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;

storage = value;
OnPropertyChanged(propertyName);
return true;
}

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public event PropertyChangedEventHandler PropertyChanged;
}

我的页面.xaml

<statebutton:StateButton Text="Rotate1" x:Name="Btn1" IsPressed="{Binding Btn1Active}"   RotateImages="true" ActiveTextColor="White" ActiveBackgroundColor="Teal" TextColor="Black" BackgroundColor="Green" ActiveBorderColor="Black" LeftImage="arrowRight" ActiveLeftImage="arrowDown" RightImage="arrowRight" HeightRequest="60"  />
<statebutton:StateButton Text="Rotate2" x:Name="Btn2" IsPressed="{Binding Btn2Active}" RotateImages="true" ActiveTextColor="White" ActiveBackgroundColor="Teal" TextColor="Black" BackgroundColor="Green" ActiveBorderColor="Black" LeftImage="arrowRight" ActiveLeftImage="arrowDown" RightImage="arrowRight" HeightRequest="60" />

<statebutton:StateButton Text="click(StateButton)" HeightRequest="60" Clicked="StateButton_Clicked"/>
<Button Text="click(Button)" HeightRequest="60" Clicked="Button_Clicked"/>

MyPage.xaml.cs

public partial class MyPage : ContentPage
{
TestModel2 myViewModel;

public MyPage ()
{
InitializeComponent ();
myViewModel = new TestModel2();
BindingContext = myViewModel;
}

private void Button_Clicked(object sender, EventArgs e)
{
myViewModel.Btn1Active = !myViewModel.Btn1Active;
myViewModel.Btn2Active = !myViewModel.Btn2Active;
}
private void StateButton_Clicked(object sender, EventArgs e)
{
myViewModel.Btn1Active = !myViewModel.Btn1Active;
myViewModel.Btn2Active = !myViewModel.Btn2Active;
}
}

更新

我为StateButtons添加了一个点击事件,它也可以正常工作。您可以查看更新后的代码。

结果是: enter image description here

注意:

1.更改以下代码:

private bool _btn1Active { get; set; }
private bool _btn2Active { get; set; }

到:

 private bool _btn1Active;
private bool _btn2Active;

2。您使用的绑定(bind)方式不正确,您可以尝试以下操作:

Btn1.SetBinding(StateButton.IsPressedProperty, new Binding("Btn1Active"));
Btn2.SetBinding(StateButton.IsPressedProperty, new Binding("Btn2Active"));

或者在xaml中绑定(bind)

<statebutton:StateButton Text="Rotate1" x:Name="Btn1" IsPressed="{Binding Btn1Active}"     />
<statebutton:StateButton Text="Rotate2" x:Name="Btn2" IsPressed="{Binding Btn2Active}" />

3.注意你的BindingContext,你可以这样做:

BtnsViewModel myViewModel;

myViewModel = new BtnsViewModel();
BindingContext = myViewModel;

更多详情,你可以查看我的演示代码。

关于android - 我的 StateButton 状态没有更新我的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com