Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的状态按钮状态未更新我的视图_Android_Ios_Xamarin_Xamarin.forms - Fatal编程技术网

Android 我的状态按钮状态未更新我的视图

Android 我的状态按钮状态未更新我的视图,android,ios,xamarin,xamarin.forms,Android,Ios,Xamarin,Xamarin.forms,我正在尝试使用Xamarin.CustomControls中的StateButtons 我有两个状态按钮,我希望他们在一个二分法的方式工作 我的视图模型如下所示: Btn1.SetBinding(StateButton.IsPressedProperty, "Btn1Active"); Btn2.SetBinding(StateButton.IsPressedProperty, "Btn2Active"); BtnsViewModel myViewModel; myViewModel = n

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

我有两个状态按钮,我希望他们在一个二分法的方式工作

我的视图模型如下所示:

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

myViewModel = new BtnsViewModel();
BindingContext = myViewModel;   
private bool\u btn1Active{get;set;}
公共bool Btn1Active
{
收到
{
返回_btn1激活;
}
设置
{
如果(_btn1Active!=值)
{
_btn1Active=有效值;
NotifyPropertyChanged(“Btn1Active”);
}
}
}
私有bool_btn2Active{get;set;}
公共bool Btn2Active
{
收到
{
返回_btn2Active;
}
设置
{
如果(_btn2Active!=值)
{
_btn2Active=值;
NotifyPropertyChanged(“Btn2Active”);
}
}
}
我绑定了如下状态按钮:

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

myViewModel = new BtnsViewModel();
BindingContext = myViewModel;   
我的按钮处理程序是:


私有无效Btn_已单击(对象发送方,事件参数e)
{
var myViewModel=BindingContext作为BtnsViewModel;
myViewModel.Btn1Active=!myViewModel.Btn1Active;
myViewModel.Btn2Active=!myViewModel.Btn2Active;
}

我已经调试了很长一段时间,我看到每个属性都得到了正确的通知和更改,但是视图没有更新。

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

  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;
}
更新

我为一个
状态按钮添加了一个click事件,它也可以正常工作。
您可以检查更新的代码

结果是:

注意:

1.更改以下代码:

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

二,。您使用的绑定方式不正确,可以尝试以下方法:

Btn1.SetBinding(StateButton.IsPressedProperty, new Binding("Btn1Active"));
Btn2.SetBinding(StateButton.IsPressedProperty, new Binding("Btn2Active"));
或在
xaml中绑定

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

有关详细信息,请查看我的演示代码。

在代码中,您的问题在于绑定

换成

Btn1.SetBinding(StateButton.IsPressedProperty, new Binding("Btn1Active"));
Btn2.SetBinding(StateButton.IsPressedProperty, new Binding("Btn2Active"));
它应该会起作用

这是因为
BtnsViewModel
不是按钮的
Context
,而是按钮所在的整个页面的
Context

一个建议是,您的私有字段不必是属性。我是说这个

private bool _btn1Active { get; set; }
private bool _btn2Active { get; set; }
可以改为

private bool _btn1Active;
private bool _btn2Active;

希望这能有所帮助。-

谢谢您的回复。我的代码仍然不起作用。我认为这是StateButtons的OnClicked默认事件的一个问题,因为我用一个额外的按钮测试了您的解决方案,该按钮可以管理行为,并且可以工作。我应该怎么做?我添加了一个StateButton,它也可以正常工作。我已经更新了答案,您可以查看。非常感谢您的回复。当我对@Jessie Zhang发表评论时,我的代码仍然不起作用C
private bool _btn1Active;
private bool _btn2Active;