Windows Phone 8.1切换按钮选中和未选中的触发器

本文关键字:触发器 按钮 Phone Windows | 更新日期: 2023-09-27 18:23:54

在加载页面之前,我检查一些ToggleButton的状态数据:

 private void CheckVoteState()
    {
        var getVoteState = from state in SelectedActivity.UserVotes
                           select state.Type;
        string voteState = getVoteState.FirstOrDefault();

        if (voteState == "positive")
        {
            VoteStateText = LocalizedStrings.Get("VoteStateUnLikeText");
            VoteState = true;
        }
        else
        {
            VoteStateText = LocalizedStrings.Get("VoteStateLikeText");
            VoteState = false;
        }
    }

但我有这两个命令,当然当我的页面加载这两个指令中的一个时会触发:

 public RelayCommand LikeCommand
    {
        get
        {
            return new RelayCommand(async() =>
            {
                //Call Like Api 
                await _IDataService.VoteForProposition(_SelectedActivity.Object.Id);                   
            });
        }
    }
    public RelayCommand UnLikeCommand
    {
        get
        {
            return new RelayCommand(async() =>
            {
                //Call Unlike Api
                await _IDataService.RemoveVoteOnProposition(_SelectedActivity.Object.Id);
            });
        }
    }

我不希望它们在页面加载时被触发,而是只有在用户选中或取消选中时才被触发。

这是我的xaml方面:

 <ToggleButton Content="{Binding VoteStateText}"
                              Foreground="White"
                              Background="LightGray"
                              Margin="10,-10,10,0"
                              BorderThickness="0"
                              HorizontalAlignment="Stretch"
                              IsChecked="{Binding VoteState}">
                    <i:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Checked">
                            <core:InvokeCommandAction Command="{Binding LikeCommand}" />
                        </core:EventTriggerBehavior>                            
                        <core:EventTriggerBehavior EventName="Unchecked">
                            <core:InvokeCommandAction Command="{Binding UnLikeCommand}" />
                        </core:EventTriggerBehavior>
                    </i:Interaction.Behaviors>
                </ToggleButton>

Windows Phone 8.1切换按钮选中和未选中的触发器

过去我用_isPageLoaded布尔值解决了这个问题。只需在Page_Loaded事件处理程序中将其设置为true,然后签入您的命令。